Rascalpi
Posts: 13
Joined: Sun May 13, 2018 3:55 pm

Crontab doesn't run my .sh script (but script works standalone)

Sat May 26, 2018 8:39 pm

Hi - I have made a crontab entry:

Code: Select all

* * * * * myscript.sh
* * * * * /bin/date >> /tmp/cron_output
(the second line is for a test and this produces:

Sat 26 May 20:14:01 UTC 2018
Sat 26 May 20:15:02 UTC 2018
Sat 26 May 20:16:01 UTC 2018
Sat 26 May 20:17:01 UTC 2018
Sat 26 May 20:18:02 UTC 2018
Sat 26 May 20:19:01 UTC 2018
Sat 26 May 20:20:01 UTC 2018
Sat 26 May 20:21:02 UTC 2018
Sat 26 May 20:22:01 UTC 2018
...etc

So the crontab is running ok.
if I type myscript.sh in the terminal it runs my python code and flashes leds on a board for a few loops. It also writes to the terminal the status of two push buttons.
myscript.sh contains:

Code: Select all

#!/bin/bash
python /home/pi/control.py
(control.py being the python code that does the Leds via GPIO)
this correctly produces (for a few loops):
Button 1 not pressed
Button 2 not pressed
Button 1 not pressed
Button 2 not pressed
Button 1 not pressed
Button 2 not pressed
But I cannot get this to activate from crontab
I have tried a few suggestions from https://garyhall.org.uk/troubleshooting ... ry-pi.html
including environmental variables check - the code runs ok
I just get nothing on the leds or push button message executing myscript.sh from the crontab output
any ideas?
Thanks
Russell

MrEngman
Posts: 3844
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK

Re: Crontab doesn't run my .sh script (but script works standalone)

Sat May 26, 2018 9:07 pm

In crontab you probably need to include the directory where your script is. e.g. * * * * * /home/pi/myscript.sh or whatever it actually is.

Read item 2 in the link you posted.
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra

Please post ALL technical questions on the forum. Please Do Not send private messages.

Rascalpi
Posts: 13
Joined: Sun May 13, 2018 3:55 pm

Re: Crontab doesn't run my .sh script (but script works standalone)

Sat May 26, 2018 9:10 pm

Thanks I tried that too:

Code: Select all

* * * * * /usr/local/bin/myscript.sh
Also I added as a test for boot up:

Code: Select all

@reboot /usr/local/bin/myscript.sh
And yes, it runs on boot up!
Really puzzled.

pcmanbob
Posts: 6683
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Crontab doesn't run my .sh script (but script works standalone)

Sat May 26, 2018 9:25 pm

First off this line "* * * * * /usr/local/bin/myscript.sh" will run the script every min of every hour of every day of every month so may be this is causing a problem if you python program which your script calls never ends or is still running when you call it again.

try running your cron line like this

Code: Select all

* * * * * /usr/local/bin/myscript.sh >> /home/pi/cron_error.txt 2>&1
then look at the file cron_error.txt which you will find in /home/pi to see if it contains any information or error messages
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Rascalpi
Posts: 13
Joined: Sun May 13, 2018 3:55 pm

Re: Crontab doesn't run my .sh script (but script works standalone)(Solved)

Sat May 26, 2018 9:26 pm

Aah - I think I see the problem. The leds are flashing each minute (I had only two quick flashes so I missed them.)
The text doesn't get echoed to the terminal, but that is probably explained here:
https://unix.stackexchange.com/question ... on-jobs-go
regards
Russell

Rascalpi
Posts: 13
Joined: Sun May 13, 2018 3:55 pm

SOLVED Re: Crontab doesn't run my .sh script (but script works standalone)

Sat May 26, 2018 9:27 pm

Thanks though for your suggestion - good for future debugging
- I'll look at how to direct the text echo to the terminal.
regards
Russell

Return to “Beginners”