script running with cron



  • having a few issues getting a script to run via cron on Ubuntu Mate on a Rasbpi

    The RasbPi runs with a user not root - Display1

    the script :-

    #!/bin/bash
    export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
    
    mkdir /tmp/cron.d
    sleep 2
    echo "Testing Cron: $(date)" >> /tmp/reboot.log
    sleep 2
    notify-send Testing
    sleep 4
    chromium-browser http://bbc.co.uk/news
    sleep 3
    mkdir /tmp/cron2.d
    
    

    So this is just a test script. So when it runs I can see the folders being created and the .log file either gets created to appended to with the "testing cron <date>"

    But I don't see the notify-send or chromium.

    The reboot.sh file is located in /etc/ folder.
    If I run the file within /etc/ with ./reboot.sh it runs

    permissions on the file at the moment are
    -rwxr-xr--x 1 root root reboot.sh

    Guessing as its not running the commands it will be something simple, but I have no idea 🙂



  • Add full path to notify-send and chromium-browser binaries and test again. Also your PATH variable is missing / after =



  • @marcinozga said in script running with cron:

    Add full path to notify-send and chromium-browser binaries and test again. Also your PATH variable is missing / after =

    Sorry typo on the post my script does have it 🙂



  • @marcinozga said in script running with cron:

    Add full path to notify-send and chromium-browser binaries

    When I do a whereis notify-send I get:-

    /usr/bin/notify-send

    so just add that to the line?

    /usr/bin/notify-send Testing



  • @hobbit666 yes.



  • Still no go.
    It's Friday 16:50 and i'm off home in 10 minutes so a job for Monday 😃



  • Still having Cron job issues.

    So ignore the script at the top. I've gone to basics:-

    This is the script located here /home/display1/chrome.sh
    (this has been created with nano chrome.sh NOT sudo)

    #!/bin/bash
    sleep 5
    pkill -o chromium
    sleep 10
    chromium-browser https://www.bbc.co.uk &
    sleep 5
    xdotool key F11
    

    ran chmod +x chrome.sh to add execute

    if I ./chrome.sh it runs and chrome opens.



  • Now if I run crontab -e again no sudo (not sure if I should or not as the script will run with the user logged in

    
    15 15 * * * /home/display1/chrome.sh
    
    

    when that time comes nothing happens.



  • @hobbit666 said in script running with cron:

    Still having Cron job issues.

    So ignore the script at the top. I've gone to basics:-

    This is the script located here /home/display1/chrome.sh
    (this has been created with nano chrome.sh NOT sudo)

    #!/bin/bash
    sleep 5
    pkill -o chromium
    sleep 10
    chromium-browser https://www.bbc.co.uk &
    sleep 5
    xdotool key F11
    

    ran chmod +x chrome.sh to add execute

    if I ./chrome.sh it runs and chrome opens.

    Add the full path for every command in the script. Use which $command to find any you don't know (I don't remember offhand where all these things live.)



  • So I've added the following to the cron job

    07 16 * * * /home/display1/chrome.sh > /home/display1/chrome.log 2>&1
    

    Also changed the chrome.sh to just 3 lines

    #!/bin/bash
    sleep 10
    /usr/bin/chromium-browser
    sleep 5
    

    So when it runs I look at the log file and see this:-
    (Chromium-browser:3494): Gtk-WARNING **: 16:07:12.000: cannot open display:



  • @hobbit666 said in script running with cron:

    So I've added the following to the cron job

    07 16 * * * /home/display1/chrome.sh > /home/display1/chrome.log 2>&1
    

    Also changed the chrome.sh to just 3 lines

    #!/bin/bash
    sleep 10
    /usr/bin/chromium-browser
    sleep 5
    

    So when it runs I look at the log file and see this:-
    (Chromium-browser:3494): Gtk-WARNING **: 16:07:12.000: cannot open display:

    Ah, there we go. The shell it's running as doesn't have a graphical environment setup. I forget offhand how I got around this because I so rarely have to. The only recent cases I've used fed to feed images to a screen.



  • @travisdh1 Yeah think I may have found the solutions.
    Will test and post back



  • Looks like I needed to put

    export DISPLAY=":0"

    originally when I was trying it I didn't have the "

    Now to test the whole script 😃