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

Community
  • 1
  • 1
rodling
  • 988
  • 5
  • 18
  • 44
  • Check `cron` is actually running - it might not be for sufficiently but down VM images. – Douglas Leeder Aug 29 '13 at 14:49
  • have you check your PYTHONPATH on .bashrc? – ayyayyekokojambo Aug 29 '13 at 14:53
  • @AlperTokgöz how do i check with .bashrc? i check python version it is 2.7 and all my dependencies are for 2.7. I run the command as it is in bash no problem, its just when i add cron notation and put it into crontab it screws up – rodling Aug 29 '13 at 14:59
  • @DouglasLeeder i checked with sudo crontab -l, any other way i should check? I am not too proficient with PIDs so couldn't check that – rodling Aug 29 '13 at 15:00
  • @rodling if your code use another module of yours, it should be added in pythonpath in .bashrc. if you don't have idea about .bashrc problem is not interested with it. forget my comments. * you can add basic python code to your crontab, like print a string to file. it may help you to specify the problem. – ayyayyekokojambo Aug 29 '13 at 15:05
  • 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. – Emmet Aug 29 '13 at 15:05
  • @Emmet 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? – rodling Aug 29 '13 at 18:08
  • 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. – Emmet Aug 29 '13 at 18:18
  • @Emmet with shebang line i still need to do `0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1` with `python` or no? – rodling Aug 29 '13 at 18:56
  • @rodling: No, if your script contains the correct shebang line and is executable (e.g. chmod 755 code1.py), it can be invoked directly. There is no need to invoke the interpreter with the script's filename as an argument. – Emmet Aug 29 '13 at 18:59
  • 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. – beroe Aug 29 '13 at 19:27
  • @rodling `ps aux | grep [c]ron` should show a running process if cron is enabled. – Douglas Leeder Aug 29 '13 at 19:29
  • If the error message is correctly copy/pasted, you have somehow managed to put an extra literal space or other invisible character after `code1.py`. I don't understand how exactly this could happen, but make sure there is just a Unix newline at the end of that line in your crontab. – tripleee Sep 01 '13 at 21:06
  • @beroe It updates DB which is very visible to me. I updated the post with email output – rodling Sep 02 '13 at 03:07
  • @tripleee i concluded that is most likely case, and i looked into that extensively. I edited the file with pressing enter in notepad, no luck. When i create new line in vi after the .py, it gives me bad minute error – rodling Sep 02 '13 at 03:08
  • Sounds like you need to remove the next line in vim as well then. And sounds like I'm on to something. – tripleee Sep 02 '13 at 03:12
  • @tripleee in vim it doesnt seem to have a new line `MAILTO=my@email */3 * * * * /opt/python2.7/bin/python /home/ec2-user/insider_data_pull.py ~` – rodling Sep 02 '13 at 17:06
  • how are you modifying the cron file? Which command are you using? – LtWorf Sep 02 '13 at 17:42
  • @LtWorf i initially did with notepad, than moved to vim – rodling Sep 02 '13 at 18:24
  • 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.) – Chris Wesseling Sep 02 '13 at 20:08
  • @ChrisWesseling i moved towards vim and -e already – rodling Sep 02 '13 at 21:25

5 Answers5

6
  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.
Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • email works Finally got a bug, it says `/bin/sh: /homeec2-user/code1.py` `: No such file or directory` – rodling Aug 29 '13 at 23:01
  • @rodling Really `/home/ec2-user/code1.py` - looks like that's missing a `/` - `/home/ec2-user/code1.py` – Douglas Leeder Aug 30 '13 at 06:05
  • that was just my mistake not in actual code. It has the correct `/` in the code – rodling Aug 30 '13 at 15:05
  • 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 – rodling Sep 02 '13 at 16:15
  • i got `MAILTO=""^M$ */3 * * * * /opt/python2.7/bin/python /home/ec2-user/code1.py^M$` – rodling Sep 02 '13 at 19:06
  • 2
    @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` – Chris Wesseling Sep 02 '13 at 20:23
  • @ChrisWesseling thanks! I rewrote the crontab from scratch with `crontab -e` after `-r` and it now works! – rodling Sep 03 '13 at 14:22
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
  • 10,470
  • 8
  • 40
  • 62
Thor
  • 158
  • 1
  • 1
  • 8
  • 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 Aug 29 '13 at 16:43
  • 2
    @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` – Emmet Aug 29 '13 at 18:20
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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 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` – rodling Sep 02 '13 at 17:03
0

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

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

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
Arnaud Bouchot
  • 1,885
  • 1
  • 21
  • 19