Clean shutdown/restart from terminal in 10.7+ I have a headless system that is controlled remotely that I need to shutdown/restart in an automated way (without the use of VNC or local keyboard/mouse).

Using various commands via SSH or via my daemon work as expected and restart the machine:

sudo shutdown -r now or osascript -e 'tell application "System Events" to restart'

However these commands cause the OS to reopen windows of any running applications even though the following are all true:


When logging out normally Reopen windows when logging back in is unchecked
Only the item I want to start with the login is listed in System Preferences &gt; Users &amp; Groups &gt; Login Items.
defaults write -g ApplePersistence -bool no is set
System Preferences &gt; General &gt; Ask to keep changes when closing documents is checked or unchecked
System Preferences &gt; General &gt; Close windows when quitting an app is checked or unchecked
Recent Items is set to any value (has no effect)
~/Library/Preferences/com.apple.loginwindow.plist contains TALLogoutSavesSate set to NO


In my case I can't actually use the applescript since 10.7+(?) warns when another machine is network shared with AFP or SMBed causing the shutdown to fail. The shutdown command forces the restart without popping up a dialog which is exactly what I need. However when the machine starts up I want exactly one application to start at login, and it is listed in Login items. 

The machine is set to automatically log in so this application can run but if any other windows were open (Terminal or TextEdit with unsaved changes) want my shutdown/restart script to force close any open applications without saving and NOT open on next login. I also would rather not for-loop through all the user processes sending each a SIGTERM or SIGKILL manually before sending the shutdown command.

I am fully aware this may cause data loss (if the user did not save their document) but this this is typically an unattended machine that I want a restart to take the machine to a known state, not the state it was left in.

AFAICT none of these work to cleanly shutdown/restart the machine in 3.7+.
While writing up this question I made a discovery that might help others to almost fully solve this issue:


  
  ~/Library/Preferences/com.apple.loginwindow.plist contains TALLogoutSavesSate set to NO
  


While this is true, there is another file located at ~/Library/Preferences/ByHost/com.apple.loginwindow.*.plist that contains an array called TALAppsToRelaunchAtLogin. That array contains a list of app windows currently open. 

Deleting this array, or in a more blunt way the whole file, and immediately restarting almost completely fixed the issue.

The remaining issue is when the machine starts up any open Finder windows are restored and if you had one or more terminal windows and actually ran the shutdown command from that window it causes it to be restored when Terminal is started again, but via SSH or a daemon seems to work fine.

I don't really think this a suitable solution in the long run but so far in my little testing it does seem to work.

upon further research it seems you can hook a script into login so it always deletes the file:
defaults write com.apple.loginwindow LoginHook /usr/bin/loginfix.sh

:edit, along the same lines as the terminal window it looks like each individual app is saving it's state, so for example if you open TextEdit, make some changes and then restart the machine using the above method when the you log back in and start TextEdit you end up with the document you had open. Looking in ~/Library/Containers/com.apple.TextEdit explains everything