Auto restarting my Home Server PI
This is something which, in an ideal world, should not be needed. However. I live in reality rather than fantasy land.
After my PI2 crashed a couple of times and it failed to boot from the MicroSD. I decided to implement a number of changes to see if I can make things better.
I already mentioned that I’ve upgraded it to a PI3. I also switched to a different SD card. I was using a 16GB PNY card. I’ve now switched to a 16GB Sandisk Ultra. I’m hoping that this will be a more reliable card going forward, only time will tell.
This next step that I’m going to be taking can be seen as controversial. Why? because it shouldn’t be needed. The plan is to instigate a weekly reboot of the PI3. This will mean that once a week or maybe even once a month, the PI will do a restart. Doing this will make sure that everything that needs to will be written to the SD Card and the PI will have a chance to flush out the RAM, Page file (which I don’t think is in use anyway) and other stuff. This should help to keep it nice and stable going forwards.
So, how do you implement a weekly automated reboot?
There is a tool that is part of Linux called CRON. This is similar to the scheduled tasks in windows. This can be used to initiate a “CRON Job” on a schedule.
To edit the CRON Jobs enter
crontab -e
at the command line.
The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.
# m h dom mon dow command
# * * * * * command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
For example:
0 0 * * * /home/pi/backup.sh
This cron entry would run the backup.sh
script every day at midnight.
So for my need I’ll add
0 03 * * 2 /home/pi/scripts/restart.sh
This will trigger the restart.sh file every tuesday at 3am. Why then? because I’m always asleep. So its least likely to interurrpt me or any other network hobs.
Next create the scripts folder if it doesn’t exist.
mkdir /home/pi/scripts
Then we’ll create the restart.sh using the Nano text editor.
nano /home/pi/scripts/restart.sh
The content of the restart.sh will look like this.
sudo reboot
Then to ensure that there’s no permission issues execute this command
chmod 755 /home/pi/scripts/restart.sh
That should be all that you need to do.