Topic: #!-friendly Sleep Timer and Alarm (GUI)
As part of learning Bash, I wrote a little sleep timer to emulate those found on TV's. The script has taken many forms, and has gained some additionally functionality. The initial post here was getting quite messy so I've simplified it to reference only the most recent script. This is the cleanest looking version, the easiest to sub-in your own commands to fit with the actions you'd like to take after the sleep timer.
By default the script depends on: bc (a command line calculator program), pm-utils (for suspend/hibernate), cvlc (command line vlc), & xscreensaver. The last three can be replaced with substitutes, by editing the relevant functions at the beginning of the script.
What the script does:
In it's simplest form it simply outputs a countdown from a defined number of minutes, and then takes a specified action. By default the following actions are included: suspend, hibernate, lock, poweroff, alarm. (To add another action simply add a function at the top, and a command line flag for it.) For example, to suspend after 40 minutes:
bashtimer.sh -s -m 40 There is also a more complex routine called "wait" which will either run a script, or watch an already running process (via its PID number) and wait for it to complete before taking an action. If no time is specified it will take the action right away, if a time is specified (say 40 minutes) it will take the action either when the passed PID/script completes, or after 40 minutes--whichever is later. For example, to suspend after running a backup script, but not an earlier than 30 minutes:
bashtimer.sh -s -w ~/bin/backup.sh -m 30 If "-w PID/script" is not specified, it will default to the normal timer mode.
If no action or time is specified it will prompt for input. Where necessary it will also prompt for a users sudo password. (Note, you can go sudo-free by switching to the dmesg commands commented out in the suspend & hibernate function and then commenting out the password-prompting bit. ) Before using the alarm, you'll want to specify the path to the sound file you'd like to play (Line 39).
The most recent version of the script can be found at: http://crunchbanglinux.org/pastebin/926
Feel free to post comments / feature requests etc. I'm still very much a newcomer to all this. Thanks to those below who have already provided very helpful feedback.
Last edited by jmbarnes (2011-01-27 21:04:15)