简体   繁体   中英

Running crontab with python

Python crontab script doesnt seem to work. When i run it manually,

python /home/ec2-user/code1.py

it works fine but when put into cron.txt file for the crontab, doesnt.

My crontab file is:

 @hourly python /home/ec2-user/code1.py >/dev/null 2>&1

i also tried

0    *    *    *    * python /home/ec2-user/code1.py >/dev/null 2>&1

But neither have much luck.

sudo crontab -l
@hourly python /home/ec2-user/code1.py >/dev/null 2>&1

Shows everything functional. I tried Crontab not running my python script and couple others with not much luck either.

EDIT:

With

PATH=/opt/python2.7/bin  
MAILTO=my@email
*/5 * * * * /home/ec2-user/code1.py

Email i get is:

 /bin/sh: /home/ec2-user/code1.py : No such file or directory

Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.

Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work and still no luck

  1. Verify cron is running: ps aux | grep [c]ron ps aux | grep [c]ron should show a running cron process
  2. Remove the redirects from the command so that cron emails you the output
  3. Add a MAILTO=<email address> to your crontab, so that you get the email
  4. Put the full path to python ( /opt/python2.7/bin/python ) instead of just python in the command
  5. Add another command to crontab such as echo FOOBAR and verify that you get the email.
  6. ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
  7. Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
  8. Run crontab -l | cat -A crontab -l | cat -A so that we can verify all the control characters are correct.

did you check the following points?

  • is your script executable? chmod 700 code1.py

  • the first line in your code should be, in most cases the python is installed at this place

    • #!/usr/bin/python

after that the crontab as follow should execute

0    *    *    *    * /home/ec2-user/code1.py >/dev/null 2>&1

If the error message is correctly copy/pasted, it seems to reveal that there is a problem with the crontab file. If you created it on a foreign platform, it might be best to start over with an empty file, this time creating it in a native editor.

As others have already pointed out, redirecting output and errors to /dev/null basically makes debugging impossible, so don't do that. If your program creates copiously verbose uninformative output, run it in a wrapper which filters out the trivial diagnostics, or, if it is your own program, rewrite it to run silently in normal operation.

Did you try "/usr/bin/python" instead of "python"?

ps ax | grep python will give you the path you could use.

try this command that should hopefully where your python is :

which python

very likely you will have something like

/usr/bin/python /home/ec2-user/code1.py

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