6

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.

3
  • 0 1 * * * /home/me/myscript is listed as a valid crontab entry Commented Apr 3, 2009 at 0:53
  • Can you give us the output of "whence root", "which root" and "find / -name root 2>/dev/null"? Debian does not allow a user to be specified in crontab so we have to assume that root is a command which is being called (see my answer). Commented Apr 3, 2009 at 6:05
  • You'll have to drop the user section. Rather su -, edit the crontab as root leaving out the username and it will be executed under that user. Commented Apr 3, 2009 at 6:55

8 Answers 8

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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?

1 Comment

1. Star stuff is pretty standard. All stars mean to execute each minute. 3. Because the larger script is in python. The contents of the python script here is just for abstraction sake. 4. Tried that. No luck. So the problem is with the cron if the log file wont even show the "hi" from the echo
1
chmod 755 /root/test.py

and then

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

should work.

Comments

0

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

1 Comment

I think the problem may be with the cron command itself because even a simple "echo hi" isn't being recorded in the log file.
0

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?

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

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