How to get PID of process by specifying process name and store it in a variable to use further? How to get PID of process by specifying process name and store it in a variable to use further? unix unix

How to get PID of process by specifying process name and store it in a variable to use further?


If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'

This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh


pids=$(pgrep <name>)

will get you the pids of all processes with the given name. To kill them all, use

kill -9 $pids

To refrain from using a variable and directly kill all processes with a given name issue

pkill -9 <name>


On a single line...

pgrep -f process_name | xargs kill -9