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
.

- 15,530
- 14
- 67
- 113

- 623
- 1
- 8
- 15
6 Answers
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,068
- 1
- 24
- 35
-
7Why have you spawned a separate super user shell to run
crontab
? Why notsudo crontab -e
? – Alex Chamberlain Oct 10 '12 at 07: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 Oct 10 '12 at 09:40 -
@MarkBooth Completely agree. IMHO
sudo
should refuse to runsu
. There will of course be a million ways around it (including-i
), but at least you will have to try harder to run aroot
shell. – Alex Chamberlain Oct 10 '12 at 09: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 Oct 10 '12 at 11:30
-
Do we need to reboot the crontab or perform any other step after editing the crontab??I remember reading somewhere that crontab needs to be reloaded or something.Please clarify if thats the case? Thanks. – SteveIrwin Oct 10 '12 at 11:50
-
1No, 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 Oct 10 '12 at 13:38 -
@scruss - Would you like to bundle up all of the comments here and edit them into your answer? That way, we can tidy up (delete) comments that no longer add anything. – Mark Booth Oct 11 '12 at 07:58
-
@scruss - Actually no, that is not considered best practice on stack exchange. Comments are there to help make the answer better. If there are elements missing from your answer which merit a response in comments you should update your answer rather than cluttering up comments. Ideally answers should have no comments, so that they don't distract from the answer. – Mark Booth Oct 11 '12 at 13:04
-
After testing, I replaced the
sudo su -
withsudo -i
. Usingsudo crontab -e
adds a confusing editor prompt for the user. – scruss Jul 07 '15 at 13:39
Did you try to add an entry with
crontab -e
Looks like you have no crontab entries and therefore the file is not created.

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

- 15,530
- 14
- 67
- 113

- 1,718
- 2
- 18
- 31
-
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 Oct 09 '12 at 23:08
-
3Don't edit the /etc/crontab directly; use the crontab commands. It's safer. – scruss Oct 09 '12 at 23:51
-
-
sudo nano /etc/crontab -e
: The-e
makes no sense, according to manpage of nano it is "Ignored, for compatibility with Pico." – macrojames Oct 10 '12 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 Oct 10 '12 at 11:49
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

- 131
- 4
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.

- 299
- 1
- 2
- 9
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

- 101
- 2
-
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 Feb 19 '24 at 17:10
mkdir /var/spool
– Elliot A. Apr 20 '17 at 23:34