Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello world Cron job not working

I am only an hour into learning how cron jobs work, and this is what I have done so far. I’m using crontab -e to add my cron command, which is:

0/1 * * * * /usr/bin/python /home/my_username/hello.py > /home/my_username/log.txt

crontab -l confirms that my command is there.

Hello.py:

#!/usr/bin/python
# Hello world python program
print "Hello World!"

But I don’t see anything in the log file. Can someone please explain what am I doing wrong?

like image 374
user1972942 Avatar asked Jan 13 '13 19:01

user1972942


1 Answers

Experiment shows that the 0/1 seems to be the problem.

0/1 should be equivalent to *. If you replace 0/1 with *, it should work.

Here's my experimental crontab:

0/1 * * * * echo 0/1  >> cron0.log
*   * * * * echo star >> cron1.log

This creates cron1.log but not cron0.log.

I'll look into this and try to figure out why 0/1 isn't working, but for now just use * and it should work.

Update:

The foo/bar syntax is specific to the Vixie cron implementation, which is used by most Linux systems and by MacOS X but is not universal.

The usual way to run a command every minute is to specify just * in the first field. To run a command every 5 minutes, if your cron supports it, specify */5.

Here's what the crontab(5) man page says:

Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the number's value through the range. For example, 0-23/2 can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is 0,2,4,6,8,10,12,14,16,18,20,22). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use */2.

I'm not even sure what 0/1 means.

UPDATE 2:

Ok, here's what I've found.

Given that fields 2 through 5 are all *, setting the first field (specifying minutes) to * causes the job to run once a minute. */2 runs every 2 minutes, and */3 runs every 3 minutes. This is all as expected.

Setting the first field to any of 0/1, 0/2, or 0/3 causes the job to run only at the top of the hour, i.e., it's equivalent to just 0.

This is not what I would have expected from the description in the man page. The Wikipedia quote in jgritty's answer:

Some versions of cron may not accept a value preceding "/" if it is not a range, such as "0". An alternative would be replacing the zero with an asterisk.

doesn't seem to be entirely correct, at least for the version of Vixie cron I'm using; the 0/1 is accepted without complaint, but it doesn't mean what I'd expect and it doesn't seem particularly useful.

like image 98
Keith Thompson Avatar answered Sep 23 '22 05:09

Keith Thompson