简体   繁体   中英

crontab bash script execution - Raspberry Pi

I have a bash script that I'm using to execute a python file with a specific version of Python (3.6). The Bash script is currently located on my desktop (/home/pi/Desktop/go.sh)

#!/bin/bash
python3.6 /home/pi/scriptDir/myScript.py

Here is my crontab entry, when I do crontab -l (note, I've deleted my other jobs)

* * * * * bash /home/pi/Desktop/go.sh # JOB_ID_3

When I run this file using the command line or from the GUI it executes properly.

When I have crontab do it, nothing happens.

Both my python file and the bash script are executable. chmod +x

Is there something obvious I'm missing?

**my python script does depend on other files in the same script directory, could that be the issue?

Here's what got it working for me. I was not using the full path to my python install. Unless you log the bash file there's no indication that you have an issue.

This is my bash file now. echo's were just to determine that I was indeed running the bash file.

#!/bin/bash
echo started
/home/pi/Python-3.6.0/python home/pi/myScriptFolder/myScript.py
echo finished

To break down the line executing the script: /home/pi/Python-3.6.0/python - is where python 3.6.0 is installed on my Pi, it could be different for you. home/pi/myScriptFolder/myScript.py is the script I want to run.

And here is my cron statement:

*/15 * * * * bash /home/pi/Desktop/go.sh > /home/pi/Desktop/clog.log 2>&1 -q -f

Breaking down this line: */15 * * * * is the cron time, in this case every 15 mins. bash /home/pi/Desktop/go.sh specifies to run a bash file and the directory of that file. > /home/pi/Desktop/clog.log 2>&1 -q -f this last section creates a log file named clog.log so you can see what's going on.

The key here was not just logging the go.sh bash file execution, but adding the 2>&1 -q -f to the end of the log request. Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file.

Cron jobs are surprisingly tricky. Aside from working directory ( which you'd have to set somewhere), you also need to handle environment setup ( $PATH , for example).

Start by redirecting your shell script's standard output and error to a log file so you can get feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM