Make an Alarmclock with the at Command and a shell

The program at is quite useful. On Unix at least—I don’t know how much of this usefulness expands to other operating systems with a program of a similar name.
The main disadvantages of at are its restrictions, build in for security reasons but not needed for every usage, especially if you know what you do. Because nobody does know what they are doing within the set of every possible consequence and their side-effects the aforementioned restrictions are hardcoded, no simple way around.

Long foreword, short problem: I wanted to avoid typing sleep 12m&& sm `date` every time I need something like a teatimer. It is also not very flexible if you want to start something at a certain time.
The program at on the other side can all that, is quite flexible and works even at high loads. The problem is, that sm is an X-program and getting it started by at is a bit tricky, although not that tricky.

The restrictions of at makes it quite dumb, it does not know every environment variable especially not the DISPLAY variable, which needs to be given explicitly. That alone is not sufficient because the X-server must allow programs started by other users (which at is) access to the screen. Access to the screen gives e.g.: xhost +localhost which is too general but the security< risk is low on a single user system. The correct way wold be xhost +username where usernam is the name of the user, of course, but the username under which at resides.

So let’s replace all of the problems with a little shell script:

#!/bin/sh
if [ $# -eq 0 ];then
  echo "Usage: $0 [in|at] time"
  exit
fi
xhost +localhost
if [ $1 = "in" ];then
  shift
  echo "env DISPLAY=$DISPLAY /usr/games/sm `date`" | at now + $*
elif [ $1 = "at" ];then
  shift
  echo "env DISPLAY=$DISPLAY /usr/games/sm `date`" | at $*
else
  echo "Usage: $0 [in|at] time"
  exit
fi
exit

Put it in a file and make it executable and put it somewhere in $PATH.
Or make a function out of it and put in your .bashrc.

Usage is simple. Let’s say the name of the file is ring.

ring in 5 min

will pop the output of date fullscreen five minutes after now.

ring at 20:14

will do the same as above but 14 minutes after eight in the evening. See the manpage of at for more information about its time format.

This script may not work at all, some reasons might be:

  • The existence of the file /etc/at.allow which does not have your username in it
  • The existence of the file /etc/at.deny which has your username in it
  • The program at is not installed
  • The program sm is not installed or at a different place, try whereis sm to find out
  • The operating system is not Unix/Unix-based. There are ports of most Unix-tools for other operating systems, try Google for a start
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s