Click here to return to the 'Quit applications politely from the command line' hint |
In the upper-left corner of the Activity Monitor window, select the “X.” Related: How to Open Any Folder from the Mac Terminal. If you’re dealing with an application that won’t respond to the previous force quit methods, then the kill command (learn more about the Kill command here) can shut the app down hard.
Very nice, thank you!
This script is well done and may be useful to some people. But automatically overwriting open files when you quit an application remotely isn't always 'polite.' Your cat might have been sleeping on the keyboard while you were away from your desk. If you send a kill signal to an application, at least you can be sure that no stored data has been overwritten.
That's a good point. Maybe I'll put out a new version with an option not to save.
I've just uploaded version 1.1 which includes an -n option that tells the app not to save changes.
Is it really such that OS X applications (which ARE processes, otherwise they wouldn't appear in 'ps' listings) do not catch SIGTERM/SIGINT and such to shut down cleanly? Really!?
To me this seems hard to imagine. But then I don't know better.
killall -QUIT TextEdit
, produces an error message saying 'The application TextEdit quit unexpectedly.' By all means feel free to play around with the various kill signals, but from what I can find, none of them perform the same way as pressing ⌘Q when using the application. Obviously every application has a process at its core, so you will see at least one process for each application, but an application is more than just a process.
I just tried it: 'killall -TERM TextEdit' works as I expect, producing no error message but just telling TextEdit to die asap. 'killall -QUIT' indeed does behave different. But SIGTERM is what's usually sent if you do not give an explicit signal.
On the other hand it's right that on SIGTERM TextEdit won't ask you for saving your half-finished document, it will discard all changes. But that's what I expect if I say 'dear program, please terminate asap'.
Now I think I understand your hint and what it's for, I got it wrong after the first read. But I am still pretty sure that most OS X programs (application or not) do handle SIGTERM and do some clean up before exiting.
killall Journler
For further reading, you might be interested in this.
This script was exactly what I was looking for. I am running an iTunes 9 Home Sharing server on an alternate account under Fast User Switching. This is serving the iTunes 'master library' for my family. The iMac it runs on is also a family computer for the kids.
Of course, I frequently need to administer the master library to keep it synced with purchases that have been loaded into individual users' libraries. With this script I'm able to ssh into the iMac FUS account, close down iTunes, then open the same iTunes library on my laptop without interrupting anyone's iMac session.
If the current iMac user is running a second copy of iTunes, this script doesn't seem to see it; it closes down the FUS iTunes session without asking me to choose between the processes. The script does generate a warning message (twice):
_RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.
but this doesn't seem to interfere with its successful execution.
Usage: quit [-a] [-p] [-s|n] application names
Arguments are the names of one or more applications.
Arguments are not case sensitive.
Arguments with spaces should be quoted.
Options:
-a Match argument string with any of the application's name, displayed name, short name, or title. E.g.: `quit 'Microsoft Word'` and `quit -a Word` will both quit Microsoft Word, because the app calls itself 'Word' in the menu bar.
-p Use partial matches (e.g. edit for TextEdit). Prompts for confirmation.
-s Attempt to save modified files when quitting. Overrides any previous -n option.
-n Do not attempt to save modified files when quitting. Overrides any previous -s option.
If neither the -s or -n options are specified, quit will try to detemine if any documents require saving, and if necessary will ask the user whether to save or not.
This script is super useful and helped me schedule cron jobs on a kiosk running Snow Leopard. It was the only thing that would let me automate the closure of an OpenOffice Impress presentation (although I had to execute 'quit' twice while in a full-screen presentation for some reason). On another note, where should I copy custom scripts to so that they can be executed globally? Or how do I modify the included path to include a folder with custom scripts? Thanks!