How can I close apps properly/shutdown the system via ssh?

I need to know if there's a proper way to close apps via ssh and if it's posible shut down the system without the sudo halt or sudo init 0 (because I think those ways are not good for the system when there are apps running)


Solution 1:

I have to start by saying that there is nothing wrong with using halt or shutdown from the command line.

In fact if you issue either of those commands then any running applications will get exactly the same message from the system as if you chose "Shut Down..." in the Finder.

The advantage of using sudo shutdown is that you can shutdown the system regardless of which users are logged in.

Solution 2:

To close apps, use

osascript -e "tell application \"Mail\" to quit"

Replace Mail with the name of the application you want to quit.

To shutdown correctly, use

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