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
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:
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.
use this commend:
shutdown -r hh:mm:ss
you can add this commend to /etc/rc.local ofter reset your device run again this.
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
shutdown should instead use them with their full path:
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
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
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.
rootsays that it should run as the super user who has permission to reboot.
/usr/sbin/reboot -fis the full path to the reboot command with the force flag.
>> /var/log/reboot.log 2>&1creates 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