1

Running Ubuntu 19.10

I have reviewed the wiki on crontab scripts but I am still unsure why mine is not starting. The bash script I would like to run is here:

#!/bin/bash
now_hour=$(date "+%H")

while ((now_hour < 21))
do
    start_time=$(date "+%d.%m.%Y-%H:%M:%S")
    status=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null  && echo ok || echo error)

    if [ "$status" == "ok" ]
    then
        timeout 30m vlc -vvv http://10.0.0.113:8000/stream.mjpg --sout="#std{acces=file,mux=ogg,dst=/home/whsrobotics/KestrelCam/Recordings/Kestrel_Box:$start_time.mp4}"
        echo "$start_time,recording_started" | tee -a "/home/whsrobotics/KestrelCam/log.txt"
    else
        echo "$start_time,network_problem" | tee -a "/home/whsrobotics/KestrelCam/log.txt"
        sleep 5m
    fi
    now_hour=$(date "+%H")
done

My crontab file is here:

# crontab -e
SHELL=/bin/bash

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

# start the Kestrel Camera recording at 5:00 am every day (will run to at least 9:00 pm)
00 05 * * * /home/whsrobotics/KestrelCam/all_day

The script works as intended when manually started in the terminal but not from cron. I have a suspicion that it may be the PATH but I am not sure how to address that. Any suggestions here are appreciated.

SteveC
  • 253
  • Try changing cron line to 00 05 * * * /bin/bash /home/whsrobotics/KestrelCam/all_day – Raffa Apr 11 '20 at 23:58
  • 1
    The first thing I would do is redirect the output/error from the crontab to a log file - ex. append >/home/whsrobotics/KestrelCam/cron.log 2>&1 - otherwise we're all guessing – steeldriver Apr 12 '20 at 00:07
  • 1
    The cron.log file contains these lines repeatedly: timeout: failed to run command ‘vlc’: No such file or directory 11.04.2020-18:12:01,recording_started – SteveC Apr 12 '20 at 00:13
  • The second part is the echo command in the script. – SteveC Apr 12 '20 at 00:14
  • Run which vlc and try to add the full path in the output instead of just using vlc in your script. Like so timeout 30m /usr/bin/vlc -vvv http://10.0.0.11....... – Raffa Apr 12 '20 at 00:21
  • 1
    The suggestion from @Raffa seems to have gotten VLC running but it is creating a new file in the Recordings directory every second. They are not actually mp4 files but empty text files. – SteveC Apr 12 '20 at 00:33
  • while ((now_hour < 21)) should be while (("$now_hour" < 21)) – Raffa Apr 12 '20 at 00:42
  • Same result. When I run script from terminal it works but when it runs from cron it is as if it is running over and over again every second. – SteveC Apr 12 '20 at 00:48
  • The first line of the log output is "error: unable to open display" – SteveC Apr 12 '20 at 00:49
  • Probably you are using root crontab which is not advised in this case. Try using your own user crontab ie crontab -e no sudo. Or if you must use root crontab, then you need to specify the display before the command, something like this 00 05 * * * env DISPLAY=:0 /home/whsrobotics/KestrelCam/all_day – Raffa Apr 12 '20 at 01:07
  • I'm not using sudo crontab. – SteveC Apr 12 '20 at 01:09
  • @Raffa from the ARITHMETIC EVALUATION section of man bash: Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. - so now_hour is fine – steeldriver Apr 12 '20 at 01:42
  • Then try to specify a display environment. It can be :0 or :0:0 or something else. Run echo $DISPLAY to find out. – Raffa Apr 12 '20 at 01:43
  • @steeldriver I know. But GNU bash, version 5.0.3(1)-release is complaining. – Raffa Apr 12 '20 at 01:45
  • echo $DISPLAY returns :0 – SteveC Apr 12 '20 at 01:52
  • So add it as in my comment above and see if it works – Raffa Apr 12 '20 at 01:55
  • Isn't there a way to run vlc in headless mode (cvlc maybe?) for these kinds of transcoding applications? that might avoid issues with display settings – steeldriver Apr 12 '20 at 02:07
  • 1
    Thank you @Raffa. The last suggestion to add env DISPLAY=:0 solved the problem for me. – SteveC Apr 12 '20 at 03:35

0 Answers0