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)
I have to start by saying that there is nothing wrong with using
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.
To close apps, use
osascript -e "tell application \"Mail\" to quit"
To shutdown correctly, use
osascript -e "tell application \"Finder\" to shut down"