Why schedule a reboot?
I wanted to schedule the DSTAR repeater, that I manage, to reboot automatically every Monday morning. The reason for this is that sometimes Pi-Star stops working and/or freezes up. This typically happens every 2 or 3 weeks of uptime without a reboot. My best guess is that due to the log files not being deleted after rotation, that the Pi software gets “clogged up.” I’ve found settings that are supposed to rotate the logs and only keep a certain number of log files, but those do not appear to work, at least not on our setup.
Since these log files are deleted on every reboot of the Pi, the next best idea in my opinion is to schedule a late-night/early-morning reboot once a week.
I used cron to schedule a reboot. I also use cron to schedule automatic linking and unlinking of reflectors. Pi-Star uses a specialized version of cron that adds a “run as user” option.
Find the Expert Cron Editor Tab
- The first step is to log in to your Pi-Star dashboard. This can be accessed by opening a web browser and going to http://pi-star.local/ or http://pi-star/ or the IP address/host name of your Pi-Star repeater or hotspot.
- After logging in to your Pi, click on “Configuration” in the upper right of the page.
- Next in the upper right of the page click on “Expert”, then on the following page in the bottom row of links on the top of the page click on “System Cron.”
Understanding cron
- You will see a text box open with a bunch of seemingly cryptic text. Each line is a cron task that is scheduled to happen at a certain time. It should look similar to this:
- About halfway down the text box you will see the following line:
# m h dom mon dow user command - This line tells us the format of each cron task. The # symbol indicates a comment and tells cron to ignore that line.
The “m” indicates “minutes”, “h” indicates hours, “dom” indicates Day of Month, “mon” indicates month, “dow” indicates Day of Week, “user” indicates the user the the command should run as, and “command” is the command you want to run at the given time/day. Refer to the chart below for options for each position in the line.
m (Minutes) |
Enter the minute you want to run the task at or * for any minute. |
h (Hours) |
Enter the hour you want to run the task at or * for any hour. |
dom (Day of Month) |
Enter the day of the month you want the task to run at or * for any day of the month. Options: 1-31 (date of the month) Example: * = any day of the month Example: 5 = 5th of the Month |
mon (Month) |
Enter the month you want to run a task or * for any month. Options: 1 = January, 2 = February, 12 = December Example: 4 = Run in April Example: 1,6,12 = Run in January, June, & December Example: * = Run any month |
dow (Day of Week) |
Enter the day of the week you want to run the task or * for any day of the week. Options: 0 or 7 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday Example: 0,3 = run on Sunday and Wednesday Example: 7 = run on Sunday Example: * = run any day |
user | Enter the username of the user that you want to run the command. Options: pi-star, root Example: root |
command | Enter the command to run. This can be a script or a single command or multiple commands. Example: reboot Example: pistar-link unlink && pistar-link ref063_c The above example will unlink the repeater and then link to REF063C. |
- Normally when you add a cron task it can be anywhere in the cron file, new lines are typically added at the end. In Pi-Star this works too, except that for some reason a line containing the reboot command should be put in as the first cron task in the list, right below the line we see in step 2 under “UNDERSTANDING CRON”.
- Note that if you add anything to the end of the file that you must maintain the new blank line at the end of the file. If you’re at the end of the last cron line, press the down arrow on your keyboard. If you see the cursor move down to the blank line, you’re all set. If the cursor doesn’t go down, just press “enter” on the keyboard to add a blank line at the end of the file.
How to Schedule a Reboot
- In order to schedule a reboot every Monday at 4:55am, I entered the following line as a cron job.
55 4 * * 1 root reboot >/dev/null 2>&1 - Don’t forget to put it at the top of the cron job list, before the first cron job line, but after the line: # m h dom mon dow user command
- Now we have a job that says, at 4:55AM every/any month, every/any day of the month, on Monday, run the reboot command as the root user, and do not show output.
- The >/dev/null 2>&1 means that whatever text output the command generates won’t be shown or logged. For more info on what this part of the command means, see this forum post on Stack Exchange.
More Info on Cron
https://www.adminschoice.com/crontab-quick-reference
https://www.freeformatter.com/cron-expression-generator-quartz.html
If you use the cron expression generator from Free Formatter, it will not work with Pi-Star, but it will give you the idea of how to write a cron expression. It also lists all the different options and has plenty of great examples of how to use cron.
Thank you for this article. It may not seem like much, but I’ve been looking for some time (when I have time) for just this bit of information and it helps avoid the Raspberry Pi freezing on me at odd times.
Cheers
You’re very welcome. I am glad you found it helpful.
Great information, very helpful. Thank you.
Hi Tyler.
I was wondering if i could apply this method to automating a task i usually have to perform in SSH?
I ordinarily update the stripped.csv database for the Nextion displays manually by SSH’ing into Pi-star and performing the following commands sequentially:
– wget “https://www.whatever.com/s/blahblahblah/stripped.csv?dl=0” -O /tmp/stripped.csv
– rpi rw
– sudo cp /tmp/stripped.csv /usr/local/etc/
– sudo reboot
I’ve already implemented your reboot string 7:30am (30 7 * * * root reboot >/dev/null 2>&1)
I was hoping i could force the System Cron feature to download the .csv file at say 7:15am previous to the scheduled reboot. Could you suggest the correct code to do this for me please?
15 7 * * * __________________
Many thanks….Richie.
I wanted to let you know that I emailed you, however I’ll post this here too in case someone else wants to do this.
cd ~/
rpi-rw
wget https://github.com/TylerDMorris25/N3TDM-Capstone/raw/master/update_stripped.sh -O update_stripped.sh
sudo chmod +x update_stripped.sh
Then in cron, add this line:
15 7 * * * root /home/pi-star/update_stripped.sh
The script contains the “reboot” command, so you can remove your reboot cron job. Or you can remove the reboot line from the script.
You could use this line in cron if you wanted to remove “reboot” from the script.
15 7 * * * root /home/pi-star/update_stripped.sh && reboot
Be sure to edit the wget URL in the script before using it.
73,
Tyler N3TDM
Hi there Tyler,
Is there any chance to use the shutdown command too?
Thanks,
Rob
No problem on spelling my name. lol I fixed it. Anyway, yes, you can schedule a shutdown. If you wanted to shutdown the hotspot at 4:55am every Monday, the cron line would look like this “55 4 * * 1 root shutdown -h now >/dev/null 2>&1” That would cause the hotspot to halt and turn off at 4:55am every Monday, You would still need to unplug it from power. Doing it this way would cause the Pi to shutdown safely and then you would need to unplug it from power and plug it back in to turn it back on. There is also an option to use RF commands to turn off specific services, but that’s beyond the scope of this comment. If you would like more info on that, please don’t hesitate to contact me through my contact page and I’ll reply through email. I hope this helps. Have a great day!