23

Is it possible to re-boot my Raspberry Pi at midnight each night? I know in Linux, you'd use crontab, but I can't seem to find /etc/crontab.

1
  • Actually, you need to make the directory /var/spool first. I have no idea why. Try mkdir /var/spool
    – Elliot A.
    Commented Apr 20, 2017 at 23:34

6 Answers 6

33

To edit the root crontab:

sudo -i
crontab -e

put the entries you want in; there's a handy template loaded by crontab that shows you what fields are what. Once you're done and saved out of the crontab editor:

exit

to get back to the user shell.

To reboot the machine at midnight and 8 am, you need the line:

0 0,8 * * * reboot

though really, Linux doesn't need to be rebooted much, if at all.

9
  • 7
    Why have you spawned a separate super user shell to run crontab? Why not sudo crontab -e? Commented Oct 10, 2012 at 7:45
  • 2
    @AlexChamberlain - Yup, it's better not to encourage people to start a root shell unless it is absolutely necessary, but at least scruss suggested exit when you are done.
    – Mark Booth
    Commented Oct 10, 2012 at 9:40
  • @MarkBooth Completely agree. IMHO sudo should refuse to run su. There will of course be a million ways around it (including -i), but at least you will have to try harder to run a root shell. Commented Oct 10, 2012 at 9:43
  • I spawned a new root shell because sudo doesn't quite give you all of root's privileges and environment. I wanted to make sure that the user was really editing root's crontab. Cron is tricksy enough with its SHELL and PATH settings, so I wanted one fewer thing to go possibly wrong.
    – scruss
    Commented Oct 10, 2012 at 11:30
  • 1
    No, the new crontab is installed as soon as you exit the editor. To check, do crontab -l. But really, I'm still confused why you need to reboot so frequently. Linux doesn't need it, and unless you're testing new kernels, you can keep the system up. My home server has been up for 415 days, happily chugging along without issues.
    – scruss
    Commented Oct 10, 2012 at 13:38
6

Did you try to add an entry with

 crontab -e

Looks like you have no crontab entries and therefore the file is not created.

5

Hopefully it will help.

sudo nano /etc/crontab -e

In the file, add a line

0 0 * * * root reboot

Haven't tried but hope this helps.

5
  • thank you for the reply. Will this reboot the system at midnight? How will I set it for another time? So like, it reboots at midnight, then again at 8am? thanks :)!
    – Phorce
    Commented Oct 9, 2012 at 23:08
  • 3
    Don't edit the /etc/crontab directly; use the crontab commands. It's safer.
    – scruss
    Commented Oct 9, 2012 at 23:51
  • sudo crontab -e Commented Oct 10, 2012 at 2:33
  • sudo nano /etc/crontab -e: The -e makes no sense, according to manpage of nano it is "Ignored, for compatibility with Pico."
    – macrojames
    Commented Oct 10, 2012 at 10:09
  • yeah thanks for clearing that up.I just started with Linux and still in the learning phase.Also from the other posts that I saw in here, it looks like its a bad idea to edit the crontab directly.
    – SteveIrwin
    Commented Oct 10, 2012 at 11:49
3

I've found other answers here to be incomplete or have bugs. This is a more complete answer that addresses problems I found with other answers and contains information about how to do debugging.

Cron on my raspberry PI doesn't have /usr/sbin in the PATH when cron jobs run. If you use just reboot, cron won't be able to find the reboot command. All other answers that use reboot or shutdown should instead use them with their full path:

  • /usr/sbin/reboot
  • /usr/sbin/shutdown

I recommend passing the "force" flag (-f) to reboot so that it reboots even if some program is trying to ask users to save their work.

So that you can debug cron jobs, I recommend enabling cron logging as described at Where do Cron error message go? so that if a cron job fails you can see what happened via /var/log/cron.log.

In addition, I recommend creating a log file just for the reboot cron job by appending the output of the reboot command (although it doesn't usually have output unless there are errors rebooting) to /var/log/reboot.log. This is done by redirecting the output with >> /var/log/reboot.log 2>&1 tacked onto the end of the reboot command line.

Other answers suggest using crontab -e to edit cron tabs. I prefer to give each cron job its own file in /etc/cron.d/. To that end I create /etc/cron.d/reboot with the contents:

0 3 * * * root /usr/sbin/reboot -f >> /var/log/reboot.log 2>&1

Where:

  • 0 3 * * * says that the reboot should happen every day at 3 AM. I don't recommend running cron jobs from 1 AM to 2 AM because that hour gets omitted or repeated when the time changes for daylight savings time.
  • root says that it should run as the super user who has permission to reboot.
  • /usr/sbin/reboot -f is the full path to the reboot command with the force flag.
  • >> /var/log/reboot.log 2>&1 creates the reboot log and prevents cron from trying to send email with the output of the command.

Putting everything together into a set of commands that enables cron logging and automatically creates the cron job:

sudo sed -ri 's|#\s*cron\.\*|cron.*|g' /etc/rsyslog.conf
sudo /etc/init.d/rsyslog restart
echo '0 3 * * * root /usr/sbin/reboot -f >> /var/log/reboot.log 2>&1' | sudo tee /etc/cron.d/reboot

If you want to verify that your pi rebooted at the correct time, use the uptime command. I'm running it now at around eight-thirty in the morning and it shows that the Pi rebooted as expected five and a half hours ago:

$ uptime 
 08:37:43 up  5:35,  2 users,  load average: 0.12, 0.20, 0.18
2

https://www.raspberrypi.org/documentation/linux/usage/cron.md

or :

use this commend:

shutdown -r hh:mm:ss 

you can add this commend to /etc/rc.local ofter reset your device run again this.

0

crontab -e

Have to admit this generator really helped!

https://crontab-generator.org/

I had to do sudo reboot now even though it was done on a root shell. shrugs

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 19 at 17:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.