3

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

asked Aug 29, 2013 at 14:22
21
  • Check cron is actually running - it might not be for sufficiently but down VM images. Commented Aug 29, 2013 at 14:49
  • 1
    Leave out the redirects to /dev/null and see what email you get: cron will email stdout/stderr to the owner (in this case root). Using the full path, /usr/bin/python, is generally a good idea in crontab entries, since you can't be sure what the PATH is. Commented Aug 29, 2013 at 15:05
  • 1
    @rodling: Aha! Your problem is, most likely, that cron can't find your python interpreter. The default path for cron is usually just /usr/bin:/bin, so one alternative is to add PATH=/opt/python2.7/bin to the top of your crontab. 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. Commented Aug 29, 2013 at 18:18
  • 1
    How are you telling that your script is working or not? Does it create a file? Because you are sending your output to /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 the crontab file to get error reports. Commented Aug 29, 2013 at 19:27
  • 3
    @roding, you should only edit the crontab with crontab -e, set VISUAL or EDITOR environment variables to crontol which editor is launched. (Since you started out with notepad, I take it you don't like vim.) Commented Sep 2, 2013 at 20:08

5 Answers 5

6
+50
  1. Verify cron is running: 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 so that we can verify all the control characters are correct.
answered Aug 29, 2013 at 19:32
Sign up to request clarification or add additional context in comments.

7 Comments

email works Finally got a bug, it says /bin/sh: /homeec2-user/code1.py : No such file or directory
@rodling Really /home/ec2-user/code1.py - looks like that's missing a / - /home/ec2-user/code1.py
that was just my mistake not in actual code. It has the correct / in the code
i did ps 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 one
@roding.. it's those ^M$ that are the culprit. Start over; crontab -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 vim
|
1

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
Alex
11.2k9 gold badges43 silver badges64 bronze badges
answered Aug 29, 2013 at 15:07

2 Comments

I tried chmod and nothing happened I added #!/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?
@rodling: the shebang line doesn't go in the crontab, it goes in your script. The shebang "#!" must be the first two characters in the file (no space or blank lines before it) and the full path to the interpreter executable, not just the containing directory, must appear on the same line, e.g. #! /opt/python2.7/bin/python
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.

answered Sep 2, 2013 at 3:24

1 Comment

crontab basically doesnt see the file. I ran 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 directory
0

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

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

answered May 15, 2014 at 10:35

Comments

0

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
answered Jul 24, 2014 at 14:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.