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 > Users & Groups > Login Items
. defaults write -g ApplePersistence -bool no
is setSystem Preferences > General > Ask to keep changes when closing documents
is checked or uncheckedSystem Preferences > General > Close windows when quitting an app
is checked or uncheckedRecent Items
is set to any value (has no effect)~/Library/Preferences/com.apple.loginwindow.plist
containsTALLogoutSavesSate
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+.