Zaid
Zaid

Reputation:

Going nuts with executing python script via crontab on debian!

This is what my crontab file looks like:

* * * * *  root /usr/bin/python /root/test.py >> /root/classwatch.log 2>&1

This is what my python script looks like:

#!/usr/bin/python
print "hello"

The cronjob creates the log file. But it is empty. I am also pretty certain that the python file is not being executed.

Appreciate any help! I've been playing with it for past 4 hrs with no luck.

Upvotes: 6

Views: 9575

Answers (8)

krisdigitx
krisdigitx

Reputation: 7126

chmod 755 /root/test.py

and then

* * * * * /root/test.py >> /root/classwatch.log 2>&1

should work.

Upvotes: 1

Anthony Towns
Anthony Towns

Reputation: 2914

There are two ways to create a crontab -- per user or globally. For the global crontab (/etc/crontab) you specify the user, as per:

# m h dom mon dow user  command
17  *   *   *   *   root        cd / && run-parts --report /etc/cron.hourly

For user crontabs you don't, as per:

aj@wherever:~$ crontab -l
0 * * * * /home/aj/bin/update-foobar

To get a python script running via #! notation, you just make the script executable (chmod 755 /root/test.py), and invoke it directly, something like:

/root/test.py

If you don't want to do that, you can run it via the python interpretor by hand, like:

/usr/bin/python /root/test.py

This assumes whichever user you're running as (ie the user in /etc/crontab, or the user you're running crontab -e as) has permission to see the python script -- /root might be inaccessible to regular users, eg.

You can get a good idea of whether your script is being executed at all by adding:

import time
time.sleep(20)   # pause for 20 seconds

and then checking with "top" or "ps aux" or "pstree" to see if python's actually running.

Upvotes: 4

Mark Roddy
Mark Roddy

Reputation: 27986

Try just sending stdout to the log file, instead of both stderr and stout:
/usr/bin/python /root/test.py > /root/classwatch.log

Upvotes: 0

John
John

Reputation:

The crontab entry is correct if you're editing /etc/crontab - however if you're using your normal user's crontab (i.e. crontab -e, crontab crontabfle, etc) the root entry is syntactically incorrect.

Upvotes: 0

ojblass
ojblass

Reputation: 21620

Updated...

Replace the contents with

* * * * * date >> /tmp/foo 

Does this link help?

Delete the file it is supposed to create. Does it come back? I thought each user had his own crontab file so the user on the line is suspsect.
DId someone play a joke on you and replace the python binary with a no op?

I have to think cron isn't working right since the echo doesn't work. Did you make sure to change the output directory to /tmp with the echo?

can you do an od (octal dump) of the file and see if maybe you put a control character or a tab into the cron file?

Upvotes: 1

runfalk
runfalk

Reputation: 1996

It might be because of a job declared earlier failing due to syntax error. Can you paste your entire crontab? Your line looks good as far as I can see.

Upvotes: 0

Jay
Jay

Reputation: 42692

This works fine for me on my RHES 4 Linux box exactly as shown (NOTE: I removed the 'root' username in the crontab).

I suspect there's something wrong with the way you're installing your cron job, or the configuration of cron on your system. How are you installing this? Are you using crontab -e or some other method? Are you able to run any other cron jobs for root successfully?

Upvotes: 0

MarkusQ
MarkusQ

Reputation: 21950

Have you tried putting the script someplace else (e.g. /usr/local/bin/)?

Upvotes: 0

Related Questions