Shell fun: at(1) and leave(1)
Want to send a mail message at a pre-specified time? Want to be reminded when to leave from work? People have worked with the Unix shell for many years now and they have developed the tools to make things like that easy. Here is how to do these things with at and leave...
First our mail example. There is a nice command called at, which allows things to be run "at a specified time". You can of course view it's man page with man at, but here is a quick rundown: First, you specify the time you want a command to be run as the commands argument, in 24 hour time (without the ":"), like at 2330. You can also give it a relative time as in at + 6 hours or at 0130 + 3 days. The actual command that will be run is expected on the "standard input", so after running the at command, you can type it in (or when in a script, you can pipe it in) and type ^D (control-D) when you're done:
at + 6 hours
mail -s 'Mail from the past' betabug@example.org
This mail...
... comes from the past.
(6 hours past, to be exact.)
^D
commands will be executed using /bin/ksh
job 1213010520.c at Mon Jun 9 18:22:00 2008
Of course this isn't very comfortable, so you might want to prepare the message in advance in a text file and pipe it in:
at + 1 hours cat todo_while_im_away.txt | mail -s "I'm on my way" betabug@example.org ^D commands will be executed using /bin/ksh job 1213016478.c at Mon Jun 9 13:34:00 2008
... and now leave!
Which maybe reminds you that you need a reminder to stop that concentrated hacking session, zoom out of "the zone" and get going, because you gonna miss your train. No problem, just tell your shell at what time you will have to leave.
The usage of leave (read the man page with man leave again) is even simpler:
leave 1800 # or even: leave +0030
In the first case you want to leave at 6pm, in the second case you want "just 30 minutes more" of sweet hacking time.
How does leave remind you? It will "beep" your terminal and print "You have to leave in 5 minutes." and then "Just one more minute!", followed by "Time to leave!" on your terminal. It doesn't wait until you enter a command to get the next terminal prompt, it writes over your input right now. Leave will continue prompting, until you kill its process or log out (thus killing its process too).
Do I see that?
leave was obviously written in a time when people worked in one shell window alone. For a long time I didn't gt it to be much useful any more. But nowadays I use screen(1) a lot and have an irssi session open for IRC (and AIM through bitlbee). Inside screen, I will be visibly and audibly reminded, that there is a "bell in window 3"... and I can go and check and see that I should leave in 5 minutes. So "screen" makes "leave" useful again.