How to get pid given the process name How to get pid given the process name unix unix

How to get pid given the process name


I think it is easier to use pgrep

$ pgrep bluetoothd441

Otherwise, you can use awk:

ps -ef | awk '$8=="name_of_process" {print $2}'

For example, if ps -efhas a line like:

root       441     1  0 10:02 ?        00:00:00 /usr/sbin/bluetoothd

Then ps -ef | awk '$8=="/usr/sbin/bluetoothd" {print $2}' returns 441.


In ksh pgrep is not found. and the other solution is failing in case below is output from ps command jaggsmca325 7550 4752 0 Sep 11 pts/44 0:00 sqlplus dummy_user/dummy_password@dummy_schema

Let's check the last column ($NF), no matter its number:

$ ps -ef | awk '$NF=="/usr/sbin/bluetoothd" {print $2}'441

If you want to match not exact strings, you can use ~ instead:

$ ps -ef | awk '$NF~"bluetooth" {print $2}'4411906


You can use pidof to get all IDs of running processes with the name p_name:

pidof p_name | tr ' ' '\n' (for a vertical listing)

pkill p_name - kill all processes whith the name p_name

Make sure that you have the permission to kill them all :)


If your ps | awk solution is failing because the output of ps is not what you want, then make it so:

ps -eaf -o pid,cmd | awk '/regex-to-match-command-name/{ print $1 }'