Regular system restarts can help resolve memory leaks, clear temporary caches, and ensure your Mac runs smoothly. While macOS provides basic scheduling options via the System Settings interface, utilizing the Terminal and the crontab command offers far greater precision for system administrators managing unattended workstations or servers.
Understanding the Shutdown Command
To restart a Mac from the command line, we use the shutdown command with the -r (restart) flag. Because restarting is a system-level operation, the command requires root privileges, which means we must schedule it using the root user’s cron table.
How to Configure the Root Crontab
Follow these steps to schedule a recurring restart via Terminal:
- Open the Terminal application (found in
/Applications/Utilities/). - Access the root user’s crontab by typing the following command and pressing Return. You will be prompted for your administrator password:
sudo crontab -e - By default, macOS opens the crontab in the
vieditor. Press theikey to enter Insert mode. - Add your cron schedule followed by the restart command. The cron format uses five time fields (Minute, Hour, Day of Month, Month, Day of Week). For example, to restart the Mac every Sunday at 3:00 AM, enter:
0 3 * * 0 /sbin/shutdown -r nowNote: Always use the absolute path
/sbin/shutdownwhen executing commands from cron, as the cron environment lacks standard PATH variables. - Once you have entered the line, press the
Esckey to exit Insert mode. - Type
:wqand press Return to save (write) the file and quit the editor. - Terminal should confirm the update by displaying:
crontab: installing new crontab.
Verifying Your Scheduled Restart
To confirm that your root crontab was successfully saved, you can list its contents without entering the editor:
sudo crontab -l
Your Mac will now automatically reboot at the specified time, ensuring optimal performance without requiring manual intervention.