I\'m trying to write a bash script to find the PID of a running process then issue a kill command. I have it partially working, but the issue I face is that there may be mor
Don't know why you would ever grep for a process to kill, unless you didn't know the command name. Most modern versions of ps have the flags
-C cmdlist
Select by command name. This selects the processes whose executable name is given in cmdlist.
and
-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.
So you can just do
ps -o pid= -C commandName
Will return the pid of all processes named exactly commandName and is cleaner and faster. Or kill a loop
while read -r pid; do
kill "$pid"
done < <(ps -o pid= -C commandName)
But really, you should always just be able to do
> pkill commandName