PDA

View Full Version : Strange crontab-script interaction (bash)



Garrigus Carraig
April 6th, 2013, 05:49 PM
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 the 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

schragge
April 6th, 2013, 06:13 PM
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.
This
env DISPLAY=:0 just displays environment on the screen (or sends it by mail when run from crontab), but doesn't set $DISPLAY for firefox. Instead, do this


# Ring the alarm : launch BBC World Service in Firefox
DISPLAY=:0 firefox --new-window http://www.bbc.co.uk/radio/player/bbc_world_service

It probably doesn't ignore the set -x either, cron just sends the output of all commands by mail. Run mailx in terminal to see your mailbox, or just
more /var/mail/$USER

BTW, wouldn't using at (http://manpages.ubuntu.com/manpages/precise/en/man1/at.1.html) be simpler?

Garrigus Carraig
April 6th, 2013, 06:22 PM
Well, that was fast and easy. Thank you, schragge. I'm a little unclear on the syntax: Does DISPLAY=:0 need to be on the same line as the firefox command?

rnerwein
April 6th, 2013, 06:29 PM
hi
my crontab looks (every 2 minutes): */2 * * * * /home/richi/tmp/ff
my script ff:
# not "env" --> export
DISPLAY=:0.0
export DISPLAY
/usr/bin/firefox --new-window http://www.bbc.co.uk/radio/player/bbc_world_service

this works for me !
and another hint is: for crontab jobs - always use the full pathname !!! ;-)
good luck
ciao
ups - schragge was faster :-)

schragge
April 6th, 2013, 06:41 PM
Does DISPLAY=:0 need to be on the same line as the firefox command?There are two options: either on the same line like what I did (then it applies only to the command it prefaces), or on a separate line, then you need to export DISPLAY like what rnerwein did. In the last case, it applies to all commands in the script that follow it.

Garrigus Carraig
April 6th, 2013, 06:42 PM
Thanks, rnerwein, and thanks for the pathname tip. :)

schragge: I don't know about at; I'll look into it. I'm a noob & glad to have come this far! :D

Garrigus Carraig
April 6th, 2013, 06:58 PM
Thanks again, schragge. One more thing: Is at Ubuntu specific? :confused:

schragge
April 6th, 2013, 07:35 PM
No, it's part of the POSIX standard (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/at.html). On Debian-based distributions it can be installed with
sudo apt-get install at

Garrigus Carraig
April 6th, 2013, 08:09 PM
Haha well I've been playing around with at, and discover indeed that it does exactly what I needed. Ah well, I got some good shell script writing practice. And I've learned that at my level, anything I want to implement has probably already been implemented. :p