Results 1 to 9 of 9

Thread: Strange crontab-script interaction (bash)

  1. #1
    Join Date
    Feb 2013
    Beans
    14

    Smile Strange crontab-script interaction (bash)

    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:
    Code:
    #!/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:
    Code:
    #!/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:
    Code:
    SHELL=/bin/bash 
        PATH=~/bin:/usr/bin:/bin 
        # 
        # m h dom mon dow command 
        53 07 * * * /home/username/bin/alarmring
    Last edited by Garrigus Carraig; April 6th, 2013 at 07:10 PM.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Strange crontab-script interaction (bash)

    Quote Originally Posted by Garrigus Carraig View Post
    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
    Code:
    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
    Code:
     
        # 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
    Code:
    more /var/mail/$USER
    BTW, wouldn't using at be simpler?
    Last edited by schragge; April 6th, 2013 at 06:37 PM.

  3. #3
    Join Date
    Feb 2013
    Beans
    14

    Re: Strange crontab-script interaction (bash)

    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?

  4. #4
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Strange crontab-script interaction (bash)

    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
    Last edited by rnerwein; April 6th, 2013 at 06:32 PM. Reason: schragge was faster :-)
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Strange crontab-script interaction (bash)

    Quote Originally Posted by Garrigus Carraig View Post
    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.

  6. #6
    Join Date
    Feb 2013
    Beans
    14

    Re: Strange crontab-script interaction (bash)

    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!

  7. #7
    Join Date
    Feb 2013
    Beans
    14

    Re: Strange crontab-script interaction (bash)

    Thanks again, schragge. One more thing: Is at Ubuntu specific?
    Last edited by Garrigus Carraig; April 6th, 2013 at 07:18 PM.

  8. #8
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Strange crontab-script interaction (bash)

    No, it's part of the POSIX standard. On Debian-based distributions it can be installed with
    Code:
    sudo apt-get install at

  9. #9
    Join Date
    Feb 2013
    Beans
    14

    Re: Strange crontab-script interaction (bash)

    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •