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
5 Answers 5
- Verify cron is running:
ps aux | grep [c]ronshould show a running cron process - Remove the redirects from the command so that cron emails you the output
- Add a
MAILTO=<email address>to your crontab, so that you get the email - Put the full path to python (
/opt/python2.7/bin/python) instead of justpythonin the command - Add another command to crontab such as
echo FOOBARand verify that you get the email. ls -l /homeec2-user/code1.py? Should that be/home/ec2-user/code1.py- Only ever edit a user's crontab with
crontab -enever from another platform, or by editing the file directly. - Run
crontab -l | cat -Aso that we can verify all the control characters are correct.
7 Comments
/bin/sh: /homeec2-user/code1.py : No such file or directory/home/ec2-user/code1.py - looks like that's missing a / - /home/ec2-user/code1.py/ in the codeps command you suggested. It seems to be running. I do youse full extension for python rather than just python and I created dummy crontab Foobar, and i get it on my email along with error codes for python onecrontab -r and follow step 7. Like I commented on the question, you can set EDITOR to control which editor is launched. Notepad users tend to prefer pico over vimdid you check the following points?
is your script executable?
chmod 700 code1.pythe 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
2 Comments
#!/opt/python2.7/bin at the top, thats where my 2.7 is located. Is that correct directory or should it remove bin? Also in your crontab, the shebang removes which program to run it with so no need for python before the code directory?#! /opt/python2.7/bin/pythonIf 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.
1 Comment
ps aux | grep [c]ron and it seems to work. Program works 100% and the email simply says /opt/python2.7/bin/python: can't open file '/home/ec2-user/code1.py': No such file or directoryDid you try "/usr/bin/python" instead of "python"?
ps ax | grep python will give you the path you could use.
Comments
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
cronis actually running - it might not be for sufficiently but down VM images./dev/nulland see what email you get:cronwill email stdout/stderr to the owner (in this case root). Using the full path,/usr/bin/python, is generally a good idea incrontabentries, since you can't be sure what thePATHis.croncan't find yourpythoninterpreter. The default path forcronis usually just/usr/bin:/bin, so one alternative is to addPATH=/opt/python2.7/binto the top of yourcrontab. A better solution is probably that given by Thor, below, but the shebang line in the script must be the full path to the python interpreter, e.g.#!/opt/python2.7/bin/python, not just to the containing directory./dev/null, as @Emmet mentioned, it won't give feedback like it will when you run it "manually". If it is truly silent, maybe give it some output just to debug. Also you can add MAILTO=your.address at the top of thecrontabfile to get error reports.crontab -e, setVISUALorEDITORenvironment variables to crontol which editor is launched. (Since you started out with notepad, I take it you don't like vim.)