0

I am trying to get my Raspberry Pi B+ to run a shell script everyday at the same time, and I am trying to use Crontab to do it. I run sudo crontab -e to access my Crontab programs, and added the entry 21 16 * * * /home/pi/Documents/python/run.sh which should have executed run.sh at 9:16pm everyday, if I read the documentation correctly, then saved the Crontab file.

The problem is, Crontab will not execute the program when 9:16pm rolls around. I have saved run.sh as an executable shell script, and it runs independently of Crontab (when I simply run it from anywhere as /home/pi/Documents/python/run.sh, the program works as expected).

If it matters, the contents of the shell script are the following, which simply blink an LED on some GPIO pins. Again, the python script runs normally. I used full paths everywhere I could think to.

#!/bin/bash

clear
/usr/bin/python /home/pi/Documents/python/blinky11.py

The program blinky11.py has contents as follows, in case you think the problem lies there:

import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(1)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(1)
        return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,550):
        blink(11)
GPIO.cleanup()

Any help appreciated!

P.S. I also tried restarting Cron with sudo service cron restart and still no dice.

1
  • Also, please don't cite my question as a duplicate unless your solution to my problem is definite. I have looked extensively at this stack exchange, and have found nothing yet which has worked.
    – RJP
    Commented Aug 14, 2018 at 2:42

3 Answers 3

2

According to documentation the order of values is

  • minute
  • hour
  • day of month
  • month
  • day of week

So, that means

21 16 * * * /home/pi/Documents/python/run.sh

Will execute /home/pi/Documents/python/run.sh at 16:21 every day

note, type man 5 crontab in your raspberry pi shell to see this documentation in your raspberry pi

2
  • Good call! I should have read closer. I will try those today and get back to you, too.
    – RJP
    Commented Aug 14, 2018 at 13:26
  • Yes - this is most certainly (at least part of) the problem +1 :)
    – Seamus
    Commented Aug 14, 2018 at 14:28
2

Just to add a couple of things to @JaromandaX's correct answer:

  1. I think it's useful to direct the stderr output of a command run under cron to a file. Even if the error message is opaque, at least you know your script threw an error. You can do this by appending the following to your script: > ~/cronjoblog 2>&1. Or in your case:

21 16 * * * /home/pi/Documents/python/run.sh > ~/cronjoblog 2>&1

which will write any errors to that file (cronjoblog) in your home directory.

  1. I was also attracted to your use of clear in your bash script, and thought I'd just mention that clear is a bit of a misnomer IMHO. I say this because in many terminals it doesn't really "clear" anything, it just moves it out of sight - a bit like sweeping dirt under the carpet I've always thought. If you scroll up in your terminal, you still see all of the previous results. If you really want to "clear" your terminal, yet retain the command history, try reset instead.
1

If it doesn't work, and is not just the problem with the format of the time mentioned in the other answer, check these points:

  • The script is executable

    Run chmod a+x /home/pi/Documents/python/blinky11.py

  • The script doesn't need special environment variables

    Run env -i /home/pi/Documents/python/blinky11.py

    If the script doesn't run, see what environment variables are needed (probably PYTHONPATH)

  • Add the following line to the start of the file:

    #!/bin/bash
    exec &> /tmp/blinky11.txt
    

    See whether /tmp/blinky11.txt has been created and whether it contains useful information.

Additionally, the purpose of clear is to clear the terminal screen. A cron job is not associated with a terminal.

1
  • I have already also run chmod on it, but I will try the environment change and add a log file for it. I'll keep you updated!
    – RJP
    Commented Aug 14, 2018 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.