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.

Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113
Phorce
  • 623
  • 1
  • 8
  • 15

6 Answers6

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.

scruss
  • 9,117
  • 1
  • 25
  • 36
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.

elomage
  • 263
  • 1
  • 6
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.

Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113
SteveIrwin
  • 1,710
  • 2
  • 18
  • 31
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.

Hamed
  • 299
  • 1
  • 2
  • 10
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

Rip3rs
  • 101
  • 2