How to get the at command to work in Ubuntu

I have recently been using "sleep 6.25h; vlc alarm.mp3" as an alarm clock. Unfortunately, this approach requires me to calculate the number of hours until I have to wake up, which is error-prone late at night. I want to just specify the time when I want to wake up.

The traditional Unix way to do this is by using at, a utility that accepts the time you want some specified command to run. This nice utility has been around since probably the 1970s and sounds simple enough, but Ubuntu manages to screw it all up. You input an at job, and it even appears on the atq queue but when the time comes to run, it does not run.

The solution is documented in Ubuntu bug #94933.

Essentially, you must export the $DISPLAY environment variable manually. The following example schedules vlc to run at 7:50am:


echo export DISPLAY=$DISPLAY \&\& vlc alarm.mp3|at 0750

TIP: Use at now to immediately run the command. This is useful for testing alarms, etc.

Now I am about to enjoy my 5 hours and 55 min of sleep.

UPDATED: Here is a script I wrote because I managed to also screw up the filename (I copy/pasted the above command when there is no alarm.mp3 in my directory).


#!/bin/sh
DEFAULT_ALARM="~/alarm.mp3"
MUSIC_PLAYER="vlc"
if [ -z "$1" ]; then
echo usage: $0 time [alarmfile]
exit
fi
TIME=$1
ALARM=$DEFAULT_ALARM
if [ -n "$2" ]; then
ALARM=$2
fi
echo export DISPLAY=$DISPLAY \&\& $MUSIC_PLAYER \"$ALARM\" | at $TIME

Say you put save this script as setalarm (and have it in a directory in your PATH). Now, to set an alarm you can do:


setalarm 0755
to wake up at 07:55 AM with the default mp3 file (which you probably don't have on your system). Or,

setalarm 0800 ~/why-cooperation-with-rms-is-impossible.mp3
to wake up at 08:00 AM with the specified mp3 file.

I found vlc works best because there is no easy way to kill command-line players like mpg321 which run in the background. However, in some very rare cases vlc seems to start up with a dialog box telling you that "the vlc team does not like it when an unauthorized app goes online" whatever that is supposed to mean. In such a case, your music won't play and you will be screwed.