简体   繁体   中英

Crontab and Python prog not running completely

I am trying to get a Python script to run hourly using crontab however I cannot seem to get it to work.

The Python program runs fine and completes if I manually run it from terminal.

$ python /home/pi/Documents/Project/Base_Prog.py

My crontab is setup like so:

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h  dom mon dow   command

@reboot /usr/bin/python /home/pi/Documents/Project/Base_Prog.py

0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py

0 * * * * /usr/bin/python /home/pi/Documents/SimpleCronTest.py

Let me clarify though, I followed suggestions on this similar post. I tried the simple test script that creates an output file and that worked. However under the same crontab with the same settings, my program doesn't complete.

The interesting point is that using TOP when the script is due to start, the Python window briefly pops up before disappearing again. So I assume cron is at least sort of working.

I have added the:

#!/usr/bin/env

Python line to the top of the Base_Prog.py file. My Python program is an API scraper that finds its inputs from one file and writes the results to another, all files in the project directory have full write permissions using chmod 777.

I am at a loss as to what is causing this.

UPDATE

The output log for both the simple test and my programme in the syslog is:

Apr  2 14:29:01 raspberrypi CRON[1455]: (pi) CMD (python /home/pi/Documents/Project/Base_Prog.py)
Apr  2 14:29:01 raspberrypi CRON[1456]: (pi) CMD (python /home/pi/Documents/CronTest.py)

I think full solution is:

First add a SHEBANG line on top of your python script:

#!/usr/bin/env python

Make your script executable with chmod +x

If your script work with stdout or if you need to know what's wrong:

0 * * * * /usr/bin/python /home/pi/Documents/Project/Base_Prog.py >> tmp.log

And just to clarify:

0 * * * * -means the cron will run always when the minutes are 0 (hourly)

You need to see the output of the cron execution for any clues, not just running it from terminal. Perhaps there are some permission/ownership errors or something along those lines. From the cron execution, you can send output to a file to view:

 0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2> /home/pi/Documents/Project/Base_Prog.err

and you can view /home/pi/Documents/Project/Base_Prog.err after for clues. Or you can also have it emailed to you:

0 * * * * python /home/pi/Documents/Project/Base_Prog.py 1> /dev/null 2>&1 | mail -s "Base Program Output" you@company.com

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