views:

1028

answers:

11

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.

A: 

What is "root" on the crontab line for?

Trey Stout
Apparently the user under which it should be executed. I've tried without it too, no luck.
From my experience, each user has hir own crontab. If you "su root" and then "crontab -e", you'll be editing root's crontab; no need to specify the user inside the file.
Piskvor
A: 

Whoops; edited because I was wrong about the redirect operator.

It could be that requisite environment variables aren't being set...

Let me try on one of my Debian boxes... seems to work fine with a simple app. Are you using the Debian-provided Python? What version of Debian (etch or lenny)?

Don Werve
Came back and re-read the title, and edited my post to add new questions.
Don Werve
+1  A: 

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?

ojblass
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
A: 

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

MarkusQ
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.
+1  A: 

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?

Jay
good point about as root...
ojblass
A: 

Hey guys, this is the OP. I'm trying to login to stackoverflow with an openid without much luck(always hated openid implementation). Wish there was a simple join function.

Anyway, no luck yet. I've tried all the suggested techniques.

What I would love is if someone can get a VERY VERY BASIC cron script that works on their box. And then I'll try it on mine. That will eliminate the doubt whether it is a permission/config error or some syntax error. Just a simple cron that writes "hello" to the log file would do.

Thanks!

0 * * * * touch /tmp/foo; date >> /tmp/foo
ojblass
* * * * * date >> /tmp/foo
ojblass
wow that worked!!!!! i'm probably still far from success but this is a great step forward:) we're getting there.
isolate and assassinate...
ojblass
A: 

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.

antennen
A: 

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.

John
A: 

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

Mark Roddy
A: 

What does:

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

or (after copying /root/test.py to /tmp/test.py and setting its permissions to 777):

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

give you?

I've never seen a crontab that allows you to specify the user within the crontab file itself, they've always been stored in one file per user.

The only possibility I can think of is that you have a root command which is effectively su - root -exec .... I tried googling this (but "man root" is not a good search term, seriously, don't do that at work :-)

Try the two options given above and see how they go. Keep in mind that, if you're not editing the crontab file with "crontab -e", you'll need to send a SIGHUP to cron to get it to re-read the file.

paxdiablo
Don't use 777 permissions - 755 permission.
Jonathan Leffler
Good advice generally but I was opening up everything to increase the chance of it working. Anyway, everyone has write access to /tmp so they can delete and recreate your file regardless of file permissions.
paxdiablo
+2  A: 

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.

Anthony Towns