How graceless is shutdown

shutdown

Shutting down the machine from the command line with e.g. shutdown -h seems as if it would not be particularly graceful, but on a scale of "Pulling the power cord out of the wall" to "Going through the standard shutdown procedure from the  Menu", how graceless is shutdown -h actually?

Best Answer

Absolutely prefer shutdown -h to pulling the power cord out of the wall. shutdown provides an opportunity for macOS to cleanly exit important underlying processes and flush file caches to storage.

Avoid shutdown

Having implemented a gentle shutdown programmatically, experience taught us that you should try to avoid shutdown -h when you have logged in users and running graphical Mac applications:

shutdown is a blunt tool that will in most cases shut down your Mac. It works at a layer below the Mac's user interface and is roughly the equivalent of force quitting everything.

With shutdown, Mac applications that would have quit nicely are not given the chance. In most cases using shutdown should be fine but care needs to be taken about denying applications of the opportunity to quit nicely.

Shutting Down Your Mac Safely, dssw.co.uk

Prefer kAEShutDown

If you are able, follow Apple's advice in Technical Q&A QA1134 - Programmatically causing restart, shutdown and/or logout. This advice can be summed up as: issue an kAEShutDown AppleEvent to the system.

Be aware that using only kAEShutDown has its limitations, these are discussed in How to Shut Down Your Mac Using AppleScript.

By AppleScript or Command Line

You can use an AppleScript snippet to issue the kAEShutDown using:

tell application "Finder"
    shut down
end tell

Or using the command line tool osascript:

osascript -e 'tell application "Finder" to shut down'

Counter to the macOS Approach

In terms of non-graphical processes, shutdown is reasonable. For anything graphical or at the user level, you should avoid it. Issuing shutdown is not appropriate for regular use on macOS; as an emergency measure it is fine but not as a pre-considered solution.

Users would quickly learn to avoid an application that simply issued shutdown instead of issuing the request through the AppleEvent.