If a user is logged in, issue the Open Scripting Architecture (OSA) command from within the user session:
osascript -e 'tell application "System Events" to restart'
This can also be called from an AppleScript or Automator workflow.
If no-one is logged in, use the shutdown command line tool:
shutdown -r now
Differences in Approach
Apple’s softest approach to shutting down is via the Finder > Apple > Shut Down menu item. This approach sends graphical applications a request to quit via an AppleEvent; at this stage applications can block the shut down if data is at risk.
Non-graphical applications, those that do not understand AppleEvents, are sent a terminate signal.
Restarting is two step: a shut down, followed by immediately powering back on.
See Shutting Down Your Mac Safely that talks about the different stages of shutting down.
shutdown -h now is the most forceful approach and should avoided when a user is logged in. This is because applications will be quit with less opportunity to save unsaved work.
A restart with the shutdown tool is initiated with:
shutdown -r now
Scripting Restart and Shut Down
If you are scripting a restart or shut down, take a look at Scheduling a Shut Down on macOS, which walks through various approaches:
- built-in via Energy Saver
- third party tools like Power Manager
- AppleScript with
tell application "System Events" to restart shutdown -r now
