Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Quit applications from the command line UNIX

The recent hint to "Extend the power of 'open -a'? inspired me to write this quick hack to quit an application:

#!/bin/sh

echo | osascript <<EOF
tell application "$*"
  quit
end tell
EOF
Call it quit, save it in your $PATH (I'm a fan of $HOME/bin), make it executable (chmod 755 quit), and then, for example, quit mail will do just that.

I don't tend to put lots of error handling in programs for my own use. You could easily add all sorts of things, like checking return codes and reporting success ... but, if you're the only one to ever run it, why bother Enjoy!
    •    
  • Currently 2.40 / 5
  You rated: 2 / 5 (5 votes cast)
 
[27,091 views]  

Quit applications from the command line | 11 comments | Create New Account
Click here to return to the 'Quit applications from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Quit applications from the command line
Authored by: Chris Biagini on Jun 25, '04 10:43:19AM

How does this compare to killall -HUP foo?

Genuinely curious :)



[ Reply to This | # ]
Quit applications from the command line
Authored by: wgscott on Jun 25, '04 03:15:03PM

It quits it softly, like when you hit the quit menu option


Any idea how to get this to work remotely, where this is of real value? It used to work, but now it fails.



[ Reply to This | # ]
Quit applications from the command line
Authored by: sjk on Jun 25, '04 10:14:28PM
Read this post on the DEVONtechnologies for an example of why killall (at least with a SIGTERM signal) isn't recommended for quitting DEVONthink; the AppleScript method is suggested instead so the database is . Need to find out whether using SIGHUP (or some other signal besides SIGTERM) with killall would be equivalent since it's more convenient.

[ Reply to This | # ]
Quit applications from the command line
Authored by: chueewowee on Jan 26, '09 08:30:47PM

I didn't make an sh script after reading your advice, which works like a treat...my example was to quit MacBiff, which (very) occasionally freezes in the menubar, when the computer is overworked, and lots of communications wizzing aorund, downloads etc...mail testing..

You can't use have the 'forcequit gui' for MaBiff, and would have to find its pid in process, then quit, Your idea works in the flash of flash.
John P.



[ Reply to This | # ]
Quit applications from the command line
Authored by: santin on Jun 25, '04 03:51:57PM
For zsh users, here is a simpler approach as a function:

quit () {
        for app in $*; do
                osascript -e 'quit app "'$app'"'
        done
}
I also have a similar "relaunch" function:

relaunch () {
        for app in $*; do
                osascript -e 'quit app "'$app'"';
                open -a $app
        done
}
Place both code snippets in your .zshrc and you are set.
You can pass multiple applications as arguments, but remember to escape white spaces if the application name has multiple words:

quit System\ Preferences
or

quit "System Preferences"


[ Reply to This | # ]
Quit applications from the command line
Authored by: ZZamboni on Jun 25, '04 04:03:49PM
There was a virtually identical hint a few days ago (and I posted a virtually identical script in one of the comments there). I guess it's a generally useful thing :-)

BTW, you don't need the

echo |
before the osascript, since you are already redirecting osascript's standard input with the
<<EOF


[ Reply to This | # ]
Quit applications from the command line
Authored by: sabi on Jun 25, '04 06:31:52PM

You may be interested in my 'appswitch' utility (http://web.sabi.net/nriley/software/) - it quits applications efficiently among other features. There you can find my 'open' replacement, 'launch', as well.



[ Reply to This | # ]
appswitch
Authored by: sjk on Jun 25, '04 10:21:42PM

Ahh... thanks for that appswitch reminder, Nicholas. It (and launch) are already installed. :-)

Looks like appswitch is a good substitute for killall to use in the /usr/libexec/upsshutdown script for "safe-quitting" apps like DEVONthink.



[ Reply to This | # ]
Quit applications from the command line
Authored by: Han Solo on Jun 25, '04 11:14:51PM
Why not simply get the PID with either top or ps -cx then type kill -QUIT PID ?

If another user left something running via FUS, you'll want to type ps -cxa or ps -cx -U username , then preface the above kill command with sudo. Or better, follow this hint and subsequent comments to pause and resume from the CLI programs run by other users.

[ Reply to This | # ]

Quit applications from the command line
Authored by: dustox on Jun 26, '04 04:09:15PM

> Why not simply get the PID with either top or ps -cx then type kill -QUIT PID ?

Because typing 'quit mail' is much much easier. And applescript quit's an app cleanly, whereas the kill command may not.



[ Reply to This | # ]
Quit applications from the command line
Authored by: kupietz on Oct 01, '04 05:32:44AM

>Why not simply get the PID with either top or ps -cx then type kill -QUIT PID ?

Because surreptitiously leaving Bittorrent clients uploading in the background on your co-worker's machines for days on end is pointless if you're just going to force quit and terminate them without them being able to report your upload ratio to the tracker.

:-)



[ Reply to This | # ]