LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-05-2009, 06:49 AM   #1
HuckleSmothered
LQ Newbie
 
Registered: Nov 2009
Distribution: Arch
Posts: 6

Rep: Reputation: 0
Using a computer as an alarm clock with the 'at' command


I've been wanting to use my laptop as an alarm clock, however I also do not want it fully on for the entire night. I'm content with suspend. Luckily the RTC wakealarm feature supports suspend wake/resume (albeit not a full power on or even hibernation/thaws).

However, I'm using the 'at' command to run what I would like to have happen just a few minutes after the computer resumes. The problem comes in to play with 'at' not actually doing anything ... for hours at least. Things don't happen when I tell it to, it is delayed.

Oddly enough, an 'at' job put in for just a few minutes from now will work right on time. Even with me suspending my laptop in that time and using my script with the wakealarm to wake it.

Yet overnight, 'at' doesn't do what it should at the specified time. Today, an hour delay. Not 60 minutes exactly either. More like 56. I don't get it.

Maybe I just don't understand how 'at' works. But Google searches to specify 'at' just don't seem to bring the 'at' command into the results. Like an ecologist searching for 'hardwood' trees tends to bring up unrelated results first.

Anyway, here is the script I've been using:

Code:
#!/bin/bash

:<< Purpose
This script wakes the computer from a suspend
Not tested for hibernation
Does not work if computer is powered off, either
Purpose

:<< Problems
The only problem I have encountered with this script were caused by UTC and LocalTime
If problems arise, check output of; sudo hwclock --show; date; cat /proc/driver/rtc
These three commands all display a time (among other things) and should be compared
If there are discrepancies, this could be a reason for the alarm not being set
Problems

:<< Misc
Functions at the top,
Script at the bottom.

Written for Banshee but can easily be changed to any music player
Just find the function that sets the music alarm and change the command used
Currently uses "banshee --play"
Rhythmbox uses "rhythmbox-client --play", for instance
Misc

############################################################
####################### FUNCTIONS ##########################
############################################################

###################################################
######### REMOVES ALL ALARMS THAT ARE SET #########
###################################################
remove_AllAlarms ()
{
		remove_WakeAlarm &&
		echo "Computer wake alarm turned off"
		remove_MusicAlarm
}

###################################################
########### REMOVES JUST THE WAKE ALARM ###########
###################################################
remove_WakeAlarm ()
{
	# Needed in order to write to the alarm
	sudo chmod 666 /sys/class/rtc/rtc0/wakealarm
	echo "0" > /sys/class/rtc/rtc0/wakealarm
}

####################################################
########## REMOVES JUST THE MUSIC ALARM ############
# WARNING: Removes ALL entries from the "at" queue #
####################################################
remove_MusicAlarm ()
{
	if [ -n 'atq' ]
		then atq | cut -f1 | xargs atrm &&
		echo -e "Music alarm turned off"
	fi
}

###################################################
############# DISPLAYS THE HELP MENU ##############
###################################################
display_Help ()
{
	echo -e "
Usage:	alarm.sh [OPTION]

Options:
 -c		Clears current alarms (same as -r)
 -d		Specifies the day for the alarm
 -h		Help menu
 -r		Deletes current alarms (same as -c)
 -t		Specifies the time for the alarm (cannot not contain spaces)
 
Exit status:
 0 if alarm was set
 1 if alarm was removed
 2 if alarm not set due to normal usage
 3 if alarm not set due to abnormal problem
"
	exit 2
}

###################################################
############# RETRIEVES TIME AND DAY ##############
###################################################
get_Time ()
{
	if [ -z "$DAY" ]
		# Finds the day
		# +%F set the format to yyyy-mm-dd
		then read -p "Enter DAY for alarm: " DAY
	fi
	DATE=`date -d "$DAY" +%F`
	if [ -z "$TIME" ]
		# Finds the alarm time
		then read -p "Enter wake up time: " TIME
		echo ""
	fi
	# Pulls the epoch date/time from the time entered
	NEWALARM=`date -d ""$DATE" "$TIME" -2 minutes" +%s`
}

###################################################
############### SETS THE WAKE ALARM ###############
###################################################
set_WakeAlarm ()
{
	# Sets new wakealarm
	echo "$NEWALARM" > /sys/class/rtc/rtc0/wakealarm &&
	SETALARM=`cat /sys/class/rtc/rtc0/wakealarm`
}

###################################################
############# CONFIRMS THE WAKE ALARM #############
###################################################
confirm_WakeAlarm ()
{
	# Confirmation test
	if [ "$NEWALARM" = "$SETALARM" ]
		then echo "SUCCESS - Confirmed WakeAlarm entry"
		echo "Alarm set for `date -d "$DATE $TIME" +%d-%b-%Y\ at\ %l:%M\ %P`"
		else echo "ERROR - WakeAlarm entry not successful"
		exit 3
	fi
}

###################################################
############# SETS MUSIC PLAYER ALARM #############
###################################################
# Can easily be changed to another music player
# Edit the echo line that outputs to the music.play file
set_BansheePlay ()
{
	# Gets date value in mm/dd/yy format
	MUSICDATE=`date --date=$DAY +%D`
	# Make alarm time
	MUSICALARM="$TIME $MUSICDATE"
	echo -e "banshee --play" > /tmp/music.play &&
	# Set the Banshee play time
	at "$MUSICALARM" -f /tmp/music.play 2> /dev/null
	rm /tmp/music.play
	echo -e 'Make sure to keep music player open and queued with music\n'
	echo -e '***** ALARM IS SET *****\n'
	exit 0
}

############################################################
######################## SCRIPT ############################
############################################################

while getopts cd:hrt: OPTION
do
	case $OPTION in
		c)	remove_AllAlarms
			exit 1;;
		d)	DAY=$OPTARG;;
		h)	display_Help;;
		r)	remove_AllAlarms
			exit 1;;
		t)	TIME=$OPTARG;;
	esac
done

echo -e "\n***** BEGIN SETTING ALARM *****\n"
remove_WakeAlarm
get_Time
set_WakeAlarm
confirm_WakeAlarm
set_BansheePlay
 
Old 12-05-2009, 07:50 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
As far as I can work out from the source code, atd should always run a command at the correct time (or maybe slightly later due to system load). It checks for new jobs each time a job is scheduled, or at most once an hour, and will sleep until the next job is due to be run. Each time it checks for a job, it will run any job whose time has arrived. Timestamps are compared with an accuracy of one second.


That leaves a few options.

Your computer's clock may be inaccurate in sleep mode, and may still be readjusting when it wakes up. That would explain why you only see the problem with long sleep times, and it should be possible to verify this by comparing your computer against a real clock. In this case, the resolution would be to sync the clock immediately when you resume (possibly with ntpdate).

It could be that the whole suspend/resume process is confusing the low-level sleep() function that atd uses to sleep for an hour (or until the next job). This might explain jobs running late, but that wouldn't explain why jobs run early, as the time would still be compared before the job was run.

There could be a bug in your script that is providing a slightly incorrect time for a long delay. You can verify this by having it print out the commands as it executes them (complete with dates) or even schedule the job by hand before you sleep.

Hope that helps,

—Robert J Lee
 
1 members found this post helpful.
Old 12-16-2009, 03:24 PM   #3
HuckleSmothered
LQ Newbie
 
Registered: Nov 2009
Distribution: Arch
Posts: 6

Original Poster
Rep: Reputation: 0
Well, I had two problems.

One was the hwclock was goofy. I had not realized that 'atd' uses the rtc instead of the system clock. That helped out a lot, thank you.

BTW, you mentioned looking at the atd code, which I went looking for after you mentioned it, but where does one find that?

Two, even with my clocks setup correctly, I was still getting an hour delay. Apparently, after a suspend/resume, the atd is still on a once an hour check. It loses its own timing, in a way. Thus, I added in a suspend hook to restart atd on resume. Worked like a charm after that.

So for anyone using this, be sure to restart atd on resume ... or wait for an hour.
 
Old 12-16-2009, 07:41 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Unless you get up a different times everyday, I'd use cron. You can specify it eg to only 'alarm' Mon-Fri.
 
Old 12-18-2009, 01:04 PM   #5
HuckleSmothered
LQ Newbie
 
Registered: Nov 2009
Distribution: Arch
Posts: 6

Original Poster
Rep: Reputation: 0
While it does vary, I originally thought about cron. I wasn't able to get it to play anything in Banshee.

First, it did nothing.

Second, I tried using a command like export DISPLAY=0: banshee --play, but alas, this would only bring up a new instance of banshee even when one was already active.

I've tried multiple variations of using the display to get cron to play something in banshee but was altogether unsuccessful.

Suggestions?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Alarm clock ColInvictus Linux - General 1 04-23-2009 11:41 PM
i need an alarm clock for 2007 bennythepitbull Mandriva 5 06-01-2007 11:39 AM
Simple Alarm Clock dudeman41465 Linux - Software 11 10-06-2006 11:33 PM
linux alarm clock? DJOtaku Linux - General 1 02-16-2005 01:41 AM
Linux Alarm Clock?? Scruff Linux - General 3 09-02-2003 09:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration