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

A script to more easily kill apps by name UNIX
I know this has been visited before, but I found one of the best methods to date for killing a process from the command line: using a variation on a script in this writeup. The secret is using the -o flag to control the output of ps. You can then use awk and grep as usual, and xargs to cycle through the results and kill all matching processes. Consider this command:
ps axco pid,command
That will output only the process ID and the command name, which makes for easy pickings for a script to find and kill! I changed the original script from the command ps e to ps axc -- -a, -x, and other flags will vary depending on the user's needs. However, do not invoke the script with the -u (e.g. ps aux), which is the way that it is normally suggested to run the ps command. The -u flag specifies the format, and overides the -o flag.

Example: to add this to a script to kill the Finder you wold do:
ps axco pid,command | grep Finder | awk '{ print $1; }' | xargs kill -9
The original article explains how to variablize it, so you could set up a generic script and run it on multiple platforms with different versions of ps by invoking:
script_name $SIG $PROCESSNAME
Remember! This script kills all processes that are named $PROCESSNAME, so it's a good idea to use ps -c, which just prints the executable name, instead of the full path. That way it won't accidently kill another process that maches a folder or something else.

There are some other tricks for using ps in that writeup as well...
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[15,295 views]  

A script to more easily kill apps by name | 7 comments | Create New Account
Click here to return to the 'A script to more easily kill apps by name' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to more easily kill apps by name
Authored by: firstianus@yahoo on May 30, '06 08:05:07AM

What about just use the killall command? See man killall.



[ Reply to This | # ]
A script to more easily kill apps by name
Authored by: purrdeta on May 30, '06 11:19:12AM

yeah killall -9 PROCESSNAME is always what I use...



[ Reply to This | # ]
Nitpicks
Authored by: thrig on May 30, '06 09:38:40AM
Two nitpicks. First: the KILL signal does not allow applications to cleanup after themselves (temporary files, sockets, child processes). kill -9 should only be used as a very last resort, or when dealing with applications known to freeze up. Second, grep ... | awk ... usually can be rewritten without the grep:
ps axco pid,command | awk '/Finder/{ print $1 }' | xargs kill
On other systems, the handy pgrep and pkill commands may replace the above. killall also works. However, be sure to consult the manual for killall before using it. Some vendors require different arguments to killall that may result in more processes being killed than expected (e.g. on Solaris).

[ Reply to This | # ]
A script to more easily kill apps by name
Authored by: geppo1982 on May 30, '06 11:19:33AM
I think killall -3 processname is the best way to kill a process by name.

[ Reply to This | # ]
A script to more easily kill apps by name
Authored by: sjk on May 30, '06 02:31:56PM
I think killall -3 processname is the best way to kill a process by name.
• That sends a SIGQUIT signal to all processes with processname.
• SIGTERM (the default for kill and killall commands on OS X) is usually the preferred signal for terminating processes.
• Especially in scripts, I recommend using the signal's shortname, e.g. killall -QUIT processname.

[ Reply to This | # ]
A script to more easily kill apps by name
Authored by: jporten on May 30, '06 11:55:03AM

I keep the following in my .cshrc (tcsh shell):

alias pid "ps -aux -ww | grep"

Ergo:

[AlBook:~] jporten% pid Safari
jporten 16477 5.2 12.0 519208 125708 ?? S 2:15PM 3:41.75 /
Applications/Safari.app/Contents/MacOS/Safari -psn_0_2090860545

Makes it very easy to get a PID for killing or top tracking.



[ Reply to This | # ]
A script to more easily kill apps by name
Authored by: wgscott on May 30, '06 01:54:03PM
Wataru Kagawa wrote a "quit" shell script (which can also be a function in zsh with an accompanying completer) to first attempt to quit the application in a friendly way, and then escalate the use of force as needed. Here are links :

[ Reply to This | # ]