Monday, July 2, 2007

按任务名称kill系列进程

A method 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...

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home