In the last several weeks, my OS X 10.8.4 laptop has gotten completely stuck in states where it's impossible to interact with directly. Several times VMWare Fusion's USB shim has broken keyboard input. One time, the system had an apparent race condition between locking the system and drawing the unlock prompt, leaving the GUI completely wedged. These are terrible problems, but there's nothing I can do to fix them.

All I can do is figure out how I can get a clean restart when this happens. I can ssh in to the computer and run any commands, without problem. I don't really trust the system state to be recoverable in either of those states, so I just want a clean reboot.

But shutdown -h now is not a clean reboot. It doesn't do the same things for cleanly shutting down GUI apps that selecting "Restart..." or "Shutdown..." from the apple menu does. It results in data loss because GUI apps weren't shut down cleanly. I could try finding everything I have running in ps, and sending them all SIGTERM. It seems to work, but it's a hugely manual process. I'd love to just tell OS X to do whatever it does when you pick those menu items.

So - is there a utility that does this right? Am I stuck doing it in a manual and error-prone way?

  • Sending SIGTERM to all processes is exactly what shutdown does :-) – nohillside Aug 14 '13 at 6:02
  • Yet, shutdown doesn't cleanly stop VMWare Fusion. Sending it SIGTERM does. Something's different. – Carl Aug 14 '13 at 6:04
  • 2
    To safely terminate GUI apps you'll need AppleScript to send a "quit" event. This might have come up before, did you do a search on the site already? – nohillside Aug 14 '13 at 6:15
  • 2
    Oh, it looks like I can use applescript to tell finder to restart directly. I'll have to test that, but it's probably right. – Carl Aug 14 '13 at 6:30

Yeah, ok. I can solve this with Applescript. Thanks to patrix for getting me to look in the right direction.

tell application "Finder"
end tell

Saved as restart.scpt, run with osascript restart.scpt. I've tested running it remotely with the screen locked, and it restarted cleanly perfectly. I need to remember that Applescript exists whenever I want to automate Finder interactions..

  • 4
    You can also use osascript -e 'tell app "System Events" to restart'. Some general scripting terminology was moved over to System Events in early versions of OS X, and the restart command is now part of the "legacy suite" in Finder's dictionary. If you look at the events tab in AppleScript Editor, both restart commands just send «event aevtrest» to loginwindow though. – Lri Aug 14 '13 at 10:57
  • killall Finder does the same thing to cause finder to restart and leave the other apps and background programs alone. – bmike Aug 14 '13 at 10:59
  • @bmike That's not the same thing at all. It is something I tested - as you said, it leaves other apps running. I don't think that fixes anything in any of the situations where I've needed this. I want a full system restart to correct whatever state has become corrupted. – Carl Aug 14 '13 at 14:53
  • Hmm - I'm a bit confused then - did your proposed finder solution where Applescript tells Finder to restart work but sending Finder a kill does not? Since you answered your own question it looks like you are ready to accept the answer as solving your problem perhaps? – bmike Aug 14 '13 at 16:46
  • @bmike The difference is telling finder to restart restarts the whole OS. Killing finder just makes the finder process respawn. – Carl Aug 14 '13 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .