简体   繁体   中英

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. 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.

path to script: /home/phil/cron_jobs/octoStatus.py

I would like cron to run every minute.

Crontab.txt file:

*/1 * * * * python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt

The octolog.txt was to capture the STDOUT and STDERR info. Output of "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. When I manually run the exact statement shown in the cron-log, everything works correctly as expected, and the octolog.txt file is created.

I am running this on FreeBSD, and I went to look at the /var/log/syslog, but it doesn't exist. I'm new to FreeBSD, but I'm not sure that means a lot in this situation, but I thought I'd mention it.

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 . Cron runs every minute by default, so:

* * * * * /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. But FreeBSD's /bin/sh is not bash . So:

* * * * * /path/to/command >/path/to/output.txt 2>&1

This sends stdout to your file, and duplicates stderr to 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.

Beware that the PATH used by cron may not include /usr/local/bin , where python is installed. If your octoStatus.py script includes a "shebang", then you may be able to execute it directly. 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). In all cases, you can get instructions as to format by reading man 5 crontab .

I would recommend you directly write to file from your python script with append mode.

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

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