I'm running Ubuntu 12.04 and bash. I've written a pair of shell scripts that allow me to set an alarm which, after ringing, unsets itself. The first, alarmset
, allows me to enter a time and modifies the alarm line in my user crontab. That line launches the second script, alarmring
, which launches a radio player in a browser window and then comments out the alarm line in the crontab.
alarmring
is behaving strangely. If I run it myself directly, it performs both actions: it launches the browser window and edits the crontab. But if I run alarmset
, when the crontab launches alarmring
at the appointed time, alarmring
edits the crontab, but does not launch the browser window.
Finally, when crontab runs alarmring
, it ignores the set -x
command, whereas when I run it directly, set -x
is executed. So it's as though the crontab is skipping the first ten lines.
Any ideas on what's going on? I'll paste the two scripts and the crontab below.
alarmset:
#!/bin/bash
# alarmset
set -x
usage()
{ echo "alarmset [ hour minute | -h ]" }
editcrontab()
{
echo $'/alarmring/s/^\(.*\)\(\* \* \*\)/'$2$' '$1$' \\2/' > ~/Documents/crontab_script.txt
crontab -l | sed --file=/home/username/Documents/crontab_script.txt > ~/Documents/new_crontab.txt crontab ~/Documents/new_crontab.txt
}
### MAIN
case $# in
2 ) editcrontab $1 $2 ;;
* ) usage
exit ;;
esac
set +x
alarmring:
#!/bin/bash
# alarmring
set -x
env DISPLAY=:0
# Ring the alarm : launch BBC World Service in Firefox
firefox --new-window http://www.bbc.co.uk/radio/player/bbc_world_service
# Unset the alarm : comment out the alarm line in the crontab
crontab -l | sed '/alarmring/s/^/#/1' > ~/Documents/new_crontab.txt
crontab ~/Documents/new_crontab.txt
set +x
crontab:
SHELL=/bin/bash
PATH=~/bin:/usr/bin:/bin
#
# m h dom mon dow command
53 07 * * * /home/username/bin/alarmring