bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Cron Job Not Running

Fri Feb 28, 2014 8:08 am

Hello,

Completely new here.
I'm trying to set up a cron job to run a python script. I already tried researching the existing threads on the subject but still cannot figure out what I am doing wrong.

I created the cron job using "sudo crontab -e".
When I type "sudo crontab -l" the changes I made are still there.
I am using full paths for the python script and am directing the output to a file (also using full path).

Here is what I put in the crontab:
45 23 * * * python /home/pi/scripts/script.py >> /home/pi/scripts/script.log

My understanding is that this should run the script everyday at 11:45 pm.
I tried both "python" by itself as well as "/usr/bin/python".
When I copy and paste the command to the unix prompt it runs just fine.

I feel like I must be doing something really stupid.

Any help is appreciated.

Thanks,
Ben

Angelus88
Posts: 442
Joined: Mon May 13, 2013 9:25 am
Location: Ivrea, TO (Italy)

Re: Cron Job Not Running

Fri Feb 28, 2014 9:57 am

Try to add & so it would be:

Code: Select all

45 23 * * * python /home/pi/scripts/script.py >> /home/pi/scripts/script.log &

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Fri Feb 28, 2014 5:20 pm

Thank you for the tip but I tried adding the "&" (to run in the background?) and that did not work either.

This morning I tried to set up the crontab from scratch and just used * in the hour field instead of the current hour to debug. For some reason that worked. When I set the minutes field to run the job in a few minutes with the hour field set to *, it works. But when I set the hour field to the current hour and the minutes field to run the job in a few minutes it does not work.

So this works:

Code: Select all

45 * * * * python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
But this doesn't work:

Code: Select all

45 7 * * * python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
Where 7 is the current hour.

So my thought was that the system clock is using the wrong time zone. But the clock on the GUI desktop is showing the correct time. I also added a statement in my python script to print the times that the script starts and finishes. When the script does run it shows the correct time.

I really don't want to run this cron job every hour. So am still looking how to get this to run at a specific time once per day. At work now. So I'll play with this more when I get home.

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cron Job Not Running

Fri Feb 28, 2014 5:24 pm

Try this:
45 7 * * * /usr/bin/python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
should run at quarter to eight every morning.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Angelus88
Posts: 442
Joined: Mon May 13, 2013 9:25 am
Location: Ivrea, TO (Italy)

Re: Cron Job Not Running

Sat Mar 01, 2014 10:40 am

DougieLawson wrote:Try this:
45 7 * * * /usr/bin/python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
should run at quarter to eight every morning.
He already tried that but with no success

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Cron Job Not Running

Sat Mar 01, 2014 3:15 pm

You have to give cron some time to process the new entry. If the current time is 08:00 then to have the cron job run in the near future, set it to run at 08:05
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron Job Not Running

Sat Mar 01, 2014 4:37 pm

It sounds to me like his system time is just plain wrong.

I'd say the first thing to try is:

* * * * * date >> /tmp/date.log

See what you get from that.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Sun Mar 02, 2014 3:51 am

* * * * * date >> /tmp/date.log

I haven't had time to work on this much. But I tried this and it output the correct date and time to the date.log file.

I just know that when I figure this out, I'm going to feel like a total idiot.

User avatar
TideMan
Posts: 265
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: Cron Job Not Running

Sun Mar 02, 2014 4:01 am

I've been where you are myself, but I cannot remember what was wrong or how I fixed it................
I seem to remember it was something to do with the paths.

Do you actually need to run this as root?
Or could you run it as pi?
In which case, you'd use:

Code: Select all

crontab -e
This will make crontab work from pi.

NorseEngineering
Posts: 1
Joined: Sun Mar 02, 2014 4:46 am

Re: Cron Job Not Running

Sun Mar 02, 2014 4:47 am

I know this is late, but to bring two threads together, make sure you chmod +x

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Mon Mar 03, 2014 4:25 am

Hi All,
Thanks for the continued tips.
I had already tried crontab -e instead of sudo crontab -e.
I already have done the chmod+x and set permissions on any subdirectories.

My latest observations and efforts:
I took the tip from Joe Schmoe about the date command and added cron jobs to run the date command 1 minute before cron jobs for my python script. I just wanted to make sure it wasn't something with the script itself.

Setting the field hour to the current hour did not work. But it seemed that when I set the hour to a future hour, the cron job worked. I haven't tested this rigorously since I don't have the time to wait around for every hour to test this hypothesis. I also didn't really feel up to messing with the system clock to test this in a more timely manner.

Early this morning I went ahead and set the cron job to run every four hours.

Code: Select all

0 6 * * * date >> /home/pi/scripts/script.log
1 6 * * *  python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
0 10 * * * date >> /home/pi/scripts/script.log
1 10 * * *  python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
0 14 * * * date >> /home/pi/scripts/script.log
1 14 * * *  python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
0 18 * * * date >> /home/pi/scripts/script.log
1 18 * * *  python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
0 22 * * * date >> /home/pi/scripts/script.log
1 22 * * *  python /home/pi/scripts/script.py >> /home/pi/scripts/script.log
I just checked the log file. The cron jobs ran at 6am, 10am, and 2pm. But at 6pm, neither the date command nor the python script ran. Argh!!

I'm not going to touch anything and see what happens at 10pm tonight (I'm in California) and 6am tomorrow morning.

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron Job Not Running

Mon Mar 03, 2014 8:42 am

You could add a bit more info to the date commands to check it is running with the correct time settings.

Something like

Code: Select all

0 6 * * * date "+(0 6) %H:%M %Z" >> /home/pi/scripts/script.log
which should print "(0 6) 06:00 PST" (or whatever your current timezone is), so you will see that the "0 6 * * *" cron entry runs at 6am.

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Tue Mar 04, 2014 5:09 am

Hopefully this is not premature but I think I figured this out.

As noted earlier I had set cron jobs to run at 6am, 10am, 2pm, 6pm, and 10pm. I also noted earlier that while the 6am, 10am, 2pm cron jobs ran. The 6pm cron job did not. On the cron jobs that did run, the "date" command output the correct local date and time of the cron job (matching system time).

When I checked again this morning, I found that the 10pm cron job also ran. But what was really weird was that the cron job ran at 2am also!!! I had not specified a cron job to run at 2am.

That's an eight hour delta between 6pm and 2am which is the same time difference between California and the UK (home of our beloved Rapberry Pi Founders.

So after some Googling, I think what happened is that cron only checks the time zone on its start up. It does not check it continuously. I must have corrected my system clock to Pacific Time Zone some time after the initial Pi setup and also after cron launched. So while my system time was Pacific, the cron was still going by GMT.

So after more Googling I executed "sudo /etc/init.d/cron restart". That seems to have done the trick to get cron to run on the same time zone as the system clock.

Thanks to all those that tried to help me out. I did pick up a couple tricks from you along the way.

User avatar
jojopi
Posts: 3272
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron Job Not Running

Tue Mar 04, 2014 12:54 pm

bpshieh wrote:So after some Googling, I think what happened is that cron only checks the time zone on its start up. It does not check it continuously. I must have corrected my system clock to Pacific Time Zone some time after the initial Pi setup and also after cron launched. So while my system time was Pacific, the cron was still going by GMT.
cron checks the local clock every minute when it wakes up.

However, if you left the system time zone /etc/localtime unchanged, and instead exported TZ somewhere, then that export will only be inherited as sessions restart. Depending on where you exported it, it may never affect cron at all.

export TZ is useful to override the time zone on a per-user basis. To set the system default time zone, use the recommended command for your distribution, such as "sudo raspi-config".

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Tue Mar 04, 2014 3:21 pm

cron checks the local clock every minute when it wakes up.

However, if you left the system time zone /etc/localtime unchanged, and instead exported TZ somewhere, then that export will only be inherited as sessions restart. Depending on where you exported it, it may never affect cron at all.

export TZ is useful to override the time zone on a per-user basis. To set the system default time zone, use the recommended command for your distribution, such as "sudo raspi-config".
The system default time zone had already been set to my local time zone when I was experiencing the issues. I didn't mention it in my previous posts, but actually I had already checked the time zone using sudo raspi-config and it was already set to my local time zone and I was still having problems. User "pi" was clearly using the local timezone. Since it was the same as the system timezone I never checked for an explicit TZ export anywhere.

Anyway, the cron restart command seems to have done the job.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron Job Not Running

Tue Mar 04, 2014 4:14 pm

The bottom line here is that the usual advice to reboot after making any system-level changes would have rendered this whole thread unnecessary.

However, we all (including, but not limited to the OP) learned something from the experience and that is, as always, a Good Thing.

PS & FWIW: I do find it a little strange that the OP did change his system time zone setting and then continued on without a reboot at any point. Not, of course, that there is anything wrong with this, but if you really think through people's usual work habits, it does seem a little odd.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
jojopi
Posts: 3272
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron Job Not Running

Tue Mar 04, 2014 8:19 pm

bpshieh wrote:Anyway, the cron restart command seems to have done the job.
Yes, you were right. The version of cron in Debian/Raspbian does seem to have some problem recognizing changes to the system time zone while it is running. (Even though it re-reads /etc/localtime every minute.)

By contrast, the version in Fedora/Pidora and Arch Linux (a fork from the same original source) behaves as I previously described, and even avoids repeating once-per-day jobs if the time(zone) is changed backwards by less than three hours. So on those systems you are slightly better not to restart cron.

bpshieh
Posts: 7
Joined: Fri Feb 28, 2014 7:54 am

Re: Cron Job Not Running

Tue Mar 04, 2014 9:29 pm

PS & FWIW: I do find it a little strange that the OP did change his system time zone setting and then continued on without a reboot at any point. Not, of course, that there is anything wrong with this, but if you really think through people's usual work habits, it does seem a little odd.
Lesson learned!!
There's a reason I posted in the Beginner Forum.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron Job Not Running

Tue Mar 04, 2014 9:38 pm

Indeed.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

farmazon3000
Posts: 36
Joined: Mon Oct 07, 2013 7:08 pm

Re: Cron Job Not Running

Fri Jan 15, 2016 9:00 am

I had the same problem - I've changed TZ but it seems that cron did not respect that change. I had to reboot and after that TZ was applied correctly and now it works.

User avatar
Nfarrow
Posts: 47
Joined: Sat Dec 20, 2014 1:24 am
Location: Oklahoma, USA
Contact: Website Facebook Twitter YouTube

Re: Cron Job Not Running

Mon Feb 15, 2016 12:10 am

Just wanting to put my two cent in for cron. I like to create a cron file in my home directory then link the crontab to the file. any other way it would create it in the /tmp folder and then when you reboot it has potential to be removed.

Code: Select all

vi crontab.2016
crontab /home/pi/crontab.2016
crontab -l
Twitter: @farrownick
Instagram veggievampire
I own a lot of Pis....

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron Job Not Running

Mon Feb 15, 2016 5:21 am

farmazon3000 wrote:I had the same problem - I've changed TZ but it seems that cron did not respect that change. I had to reboot and after that TZ was applied correctly and now it works.
cron will only pick up the changed timezone when it is restarted. A reboot will do this, or a "sudo service cron restart". There's probably a 'proper' systemd way of doing that with systemdctl, but I'm not going there yet.

Seylione
Posts: 36
Joined: Fri Aug 23, 2013 4:49 pm
Location: France

Re: Cron Job Not Running

Sun Apr 09, 2017 6:19 pm

Hi everybody,
I am lost in Cron! : To monitor my PV system I need to launch a script every hour or so, to have my Raspi+Piface digital 2 read data and sent it to my webpage.
To test it I wrote a small script that makes some of the Piface digital 2 blink, it works well under IDLE F5 so I do not put it here.
I then Chmodded it and wrote it into crontab as
* * * * * /usr/bin/python /home/pi/gpv/test_cron.py
Rebooted. It should make my Piface digital 2 blink every minute but ... Nothing

I did not find any /etc/syslog.conf

I tried to read all that post and answers but it did not help.

Where am I wrong?
Lionel, RaspberryPi 3, Piface digital 2

User avatar
DougieLawson
Posts: 39169
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cron Job Not Running

Sun Apr 09, 2017 6:36 pm

Once a minute

Code: Select all

*/1 * * * * /usr/bin/python /home/pi/gpv/test_cron.py
Once an hour

Code: Select all

* */1 * * * /usr/bin/python /home/pi/gpv/test_cron.py
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
rpdom
Posts: 17189
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron Job Not Running

Sun Apr 09, 2017 6:52 pm

DougieLawson wrote:Once a minute

Code: Select all

*/1 * * * * /usr/bin/python /home/pi/gpv/test_cron.py
This is effectively the same as

Code: Select all

* * * * * /usr/bin/python /home/pi/gpv/test_cron.py
I suspect the issue is more likely to be the directory that the script is running in.

Return to “Beginners”