不知道爲什麼你會想要殺死一個進程,除非你不知道命令名。的ps大多數現代版本具有標誌
-C cmdlist
Select by command name. This selects the processes whose executable name is given in cmdlist.
和
-o format
User-defined format. format is a single argument in the form of
a blank-separated or comma-separated list, which offers a way to
specify individual output columns. The recognized keywords are
described in the STANDARD FORMAT SPECIFIERS section below.
Headers may be renamed (ps -o pid,ruser=RealUser -o
comm=Command) as desired. If all column headers are empty (ps
-o pid= -o comm=) then the header line will not be output.
Column width will increase as needed for wide headers; this may
be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-
WCHAN-COLUMN -o comm). Explicit width control (ps opid,
wchan:42,cmd) is offered too. The behavior of ps -o pid=X,
comm=Y varies with personality; output may be one column named
"X,comm=Y" or two columns named "X" and "Y". Use multiple -o
options when in doubt. Use the PS_FORMAT environment variable
to specify a default as desired; DefSysV and DefBSD are macros
that may be used to choose the default UNIX or BSD columns.
所以,你可以做
ps -o pid= -C commandName
將返回這裏命名命令名的所有進程的PID,是更清潔和更快速。或殺死一環
while read -r pid; do
kill "$pid"
done < <(ps -o pid= -C commandName)
不過說真的,你應該永遠只能夠做
> pkill commandName
檢查'killall'或'pkill'命令來保存自己大量的工作。 –