简体   繁体   English

crontab bash脚本执行-Raspberry Pi

[英]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). 我有一个bash脚本,用于使用特定版本的Python(3.6)执行python文件。 The Bash script is currently located on my desktop (/home/pi/Desktop/go.sh) Bash脚本当前位于我的桌面上(/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) 这是我执行crontab -l时的crontab条目(注意,我已经删除了其他作业)

* * * * * 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. 当我使用命令行或从GUI运行此文件时,它将正确执行。

When I have crontab do it, nothing happens. 当我执行crontab时,什么也没发生。

Both my python file and the bash script are executable. 我的python文件和bash脚本都是可执行的。 chmod +x 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? **我的python脚本确实依赖于同一脚本目录中的其他文件,这可能是问题吗?

Here's what got it working for me. 这就是它为我工作的原因。 I was not using the full path to my python install. 我没有使用python安装的完整路径。 Unless you log the bash file there's no indication that you have an issue. 除非您记录bash文件,否则没有迹象表明您有问题。

This is my bash file now. 这是我的bash文件。 echo's were just to determine that I was indeed running the bash file. echo只是确定我确实在运行bash文件。

#!/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/Python-3.6.0/python是在我的Pi上安装python 3.6.0的位置,它可能与您有所不同。 home/pi/myScriptFolder/myScript.py is the script I want to run. home/pi/myScriptFolder/myScript.py是我要运行的脚本。

And here is my cron statement: 这是我的cron声明:

*/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. 打破这一行: */15 * * * *是计划时间,在这种情况下是每15分钟一次。 bash /home/pi/Desktop/go.sh specifies to run a bash file and the directory of that file. bash /home/pi/Desktop/go.sh指定运行bash文件以及该文件的目录。 > /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. > /home/pi/Desktop/clog.log 2>&1 -q -f这最后一部分创建了一个名为clog.log的日志文件,因此您可以查看发生了什么。

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. 此处的关键不仅在于记录go.sh bash文件的执行情况,还在于在日志请求的末尾添加2>&1 -q -f Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file. 在我这样做之前,没有任何问题的迹象,然后我将python文件错误返回到日志文件中。

Cron jobs are surprisingly tricky. Cron工作出奇的棘手。 Aside from working directory ( which you'd have to set somewhere), you also need to handle environment setup ( $PATH , for example). 除了工作目录(必须在某个目录中设置)之外,还需要处理环境设置(例如$PATH )。

Start by redirecting your shell script's standard output and error to a log file so you can get feedback. 首先将您的Shell脚本的标准输出和错误重定向到日志文件,以便您获得反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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