User avatar
geo3geo
Posts: 21
Joined: Wed Feb 10, 2016 2:55 pm

Auto reboot not working with crontab

Wed Apr 13, 2016 5:42 pm

I know this has come up before but I tried the various suggestions and nothing works. In Crontab I have the following which I think should reboot Pi and close the SSH window at 30 minutes past the hour. It works if I just type
sudo reboot -r now;exit on the command line but doesn't work in crontab.
* 30 * * * sudo reboot -r now;exit

I've tried putting sudo reboot in a myboot.sh file, chmoding and incorporating into crontab as
* 30 * * * /home/pi/myboot.sh
but that doesn't work

So I tried something simple like
* 30 * * * /home/pi/ls
and that doesn't work either!

Any ideas?

Geo

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

Re: Auto reboot not working with crontab

Wed Apr 13, 2016 6:35 pm

The first rule of debugging crontab entries is to send the output somewhere that you can check it.

Code: Select all

* 30 * * * /home/pi/ls >/tmp/output 2>/tmp/error
That will write any output messages to a file called /tmp/output and any error messages to /tmp/error, so you wait until just after 30 minutes past the hour then look at those files to see any messages.

The second rule is "don't use sudo in crontab". You shouldn't need it. If a program requires sudo "root" access, run it from the root crontab by using "sudo crontab -e". Otherwise if it doesn't need root access use the normal user crontab with "crontab -e" (no sudo).

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Auto reboot not working with crontab

Wed Apr 13, 2016 6:42 pm

If you're rebooting why do you need to close the terminal window first? It will obviously disappear when it reboots.

I'm not sure you can use "reboot", "shutdown" and similar commands from the users cron, even if you specify "sudo". It seems to vary with different *NIX distributions, so it may not work with Raspbian. I've never actually tried it, and as @rpdom pointed out it's not the way it should be done even if it works.

Since you want to reboot I'd put it in root's cron.

Code: Select all

sudo crontab -e
Then just add

Code: Select all

* 30 * * * /sbin/reboot 
The "-r" switch you have isn't doing anything, I don't think it's even a valid option for the "reboot" command. It wouldn't serve any purpose since the command is to reboot anyway, so why add a reboot option to a reboot command. Same for the "now", reboot is always "now", no need to specify a time period.

Nion
Posts: 9
Joined: Wed Apr 13, 2016 6:47 pm

Re: Auto reboot not working with crontab

Wed Apr 13, 2016 6:52 pm

It should be 30 * * * *, not * 30 * * *. Your cron entry is currently set to run on the 30th hour of the day.

User avatar
geo3geo
Posts: 21
Joined: Wed Feb 10, 2016 2:55 pm

Re: Auto reboot not working with crontab

Thu Apr 14, 2016 8:54 am

It's often the obvious that get overlooked! So I had the hour/mins crossed !. But it still took some time to get things working. So here are a few points for others who may pass this way.
For what it matters all this relates to my remote headless Raspbian Pi's, programmed via Putty on my Windows laptop.

30 * * * * date
doesn't put anything in the terminal window which led me to think nothing was happening
30 * * * * date>mydate
produces a file called mydate in my user space, so it does work after all
30 * * * * ./myreboot.sh
where myreboot.sh contains 'sudo reboot' works fine too

This may be a sledgehammer way of doing things but it works. I have two remote Pi's linked via WiFi repeaters and they often loose WiFi connectivity so rebooting every hour will sort things temporarily. I'll now look into more elegant methods!

Thanks for all the feedback, the various ideas helped lots.
Geo

User avatar
RaTTuS
Posts: 10828
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Auto reboot not working with crontab

Thu Apr 14, 2016 9:07 am

so you want to reboot the RPi every 30 mins [WTHF?]

so what you need is a root cron to do it
sudo crontab -e
30 * * * * /sbin/shutdown -r now

don't put sudo in crontabs - it's just wrong
always put fill paths to everything
it will not echo to the display or terminal unless you explicitly tell it

if you want to inform the uses of a coming shutdown then
try a
shutdown -r +10
which will reboot after 10 mins [and inform all users what is happening @ 5 mins then every min]
see
man shutdown
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Beginners”