[英]How else can I troubleshoot this cron job?
I've been working (and searching) to get this cron job / python script running for some time now. 我一直在工作(和搜索)让这个cron job / python脚本运行一段时间了。 However, it's obviously not working.
但是,它显然不起作用。 I'm not sure how else to troubleshoot why, and I've tried several things I've found here in other SO questions.
我不确定如何解决原因,我在其他SO问题中尝试了一些我在这里找到的东西。
path to script: /home/phil/cron_jobs/octoStatus.py 脚本的路径:/home/phil/cron_jobs/octoStatus.py
I would like cron to run every minute. 我希望cron每分钟都跑一次。
Crontab.txt file: Crontab.txt文件:
*/1 * * * * python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt
The octolog.txt was to capture the STDOUT and STDERR info. octolog.txt用于捕获STDOUT和STDERR信息。 Output of "sudo tail /var/log/cron"
输出“sudo tail / var / log / cron”
Jul 3 10:20:00 bsd /usr/sbin/cron[83876]: (root) CMD (/usr/libexec/atrun)
Jul 3 10:20:00 bsd /usr/sbin/cron[83877]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
Jul 3 10:21:00 bsd /usr/sbin/cron[83903]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
Jul 3 10:22:00 bsd /usr/sbin/cron[83934]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
It appears to be running each minute as desired. 它似乎按照需要每分钟运行一次。 However, the expected results of the script are not occurring.
但是,脚本的预期结果没有发生。 octolog.txt is also not being created.
octolog.txt也没有被创建。 When I manually run the exact statement shown in the cron-log, everything works correctly as expected, and the octolog.txt file is created.
当我手动运行cron-log中显示的确切语句时,一切都按预期正常工作,并创建octolog.txt文件。
I am running this on FreeBSD, and I went to look at the /var/log/syslog, but it doesn't exist. 我在FreeBSD上运行它,然后我去查看/ var / log / syslog,但它不存在。 I'm new to FreeBSD, but I'm not sure that means a lot in this situation, but I thought I'd mention it.
我是FreeBSD的新手,但我不确定在这种情况下这意味着什么,但我想我会提到它。
I'm not sure what other info would be helpful, as I'm stuck. 我不确定其他信息会有什么帮助,因为我被困住了。 Thanks.
谢谢。 Phil
菲尔
First off, to run your script every minute, you don't need */1
. 首先,每分钟运行一次脚本,你不需要
*/1
。 Cron runs every minute by default, so: 默认情况下,Cron每分钟运行一次,因此:
* * * * * /path/to/command
Next, your redirection may be broken. 接下来,您的重定向可能会被破坏。 The
bash
man page has the &>
format listed under "Redirecting Standard Output and Standard Error", so I assume that's what you're trying to do. bash
手册页的“重定向标准输出和标准错误”下列出了&>
格式,所以我认为这就是你要做的。 But FreeBSD's /bin/sh
is not bash . 但是FreeBSD的
/bin/sh
不是bash 。 So: 所以:
* * * * * /path/to/command >/path/to/output.txt 2>&1
This sends stdout to your file, and duplicates stderr to stdout. 这会将stdout发送到您的文件,并将stderr复制到stdout。
This brings us to: 这让我们:
* * * * * python /home/phil/cron_jobs/octoStatus/octoStatus.py > octolog.txt 2>&1
Note also according to man 5 crontab
, you can set a MAILTO
variable in your crontab file which will direct output/errors from your jobs to an email address. 另请注意,根据
man 5 crontab
,您可以在crontab文件中设置MAILTO
变量,该变量将把作业中的输出/错误定向到电子邮件地址。
Beware that the PATH used by cron
may not include /usr/local/bin
, where python is installed. 请注意,
cron
使用的PATH可能不包含/usr/local/bin
,其中安装了python。 If your octoStatus.py
script includes a "shebang", then you may be able to execute it directly. 如果您的
octoStatus.py
脚本包含“shebang”,那么您可以直接执行它。 Otherwise, you will either have to provide the full path to your python
binary, or add a PATH variable to your crontab (akin to the MAILTO I mentioned above). 否则,您将需要提供
python
二进制文件的完整路径,或者向您的crontab添加一个PATH变量(类似于我上面提到的MAILTO)。 In all cases, you can get instructions as to format by reading man 5 crontab
. 在所有情况下,您都可以通过阅读
man 5 crontab
来获取有关格式化的说明。
I would recommend you directly write to file from your python script with append
mode. 我建议您使用
append
模式直接从python脚本写入文件。
Also just guessing but I think you should give absolute path to output file as stated in comment something like python home/phil/cron_jobs/octoStatus/octoStatus.py &> /home/phil/cron_jobs/octoStatus/octolog.txt
也只是猜测,但我认为你应该给出输出文件的绝对路径,如评论中所述,如
python home/phil/cron_jobs/octoStatus/octoStatus.py &> /home/phil/cron_jobs/octoStatus/octolog.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.