73

Hi I have searched various forums and here as well, I could find some answers for Linux and Mac but not able to find solution for Unix and specially Korn Shell.

How to get process name (command name) from process id (pid)

Below reference I found from SO This one And this one also

I tried below command

ps -eaf | awk '{ print substr($0, index($0, $9)) }'

Above command is failing at a point where TIME is given rather than Month and Date (because in this case there will be only 8 columns in string)

Any suggestion would help.

Community
  • 1
  • 1
gahlot.jaggs
  • 1,083
  • 3
  • 11
  • 21
  • No I want pid from process name, it was wrongly type in title – gahlot.jaggs Sep 12 '13 at 13:17
  • This issue is very specific to your version of unix and has little to do with the shell. Please include the output of `uname -a`. – Henk Langeveld Sep 13 '13 at 12:04
  • For Mac users who happen to find this, see also https://stackoverflow.com/questions/11546765/how-to-get-the-pid-of-a-process-by-giving-the-process-name-in-mac-os-x. – jvriesem Aug 31 '18 at 20:36

4 Answers4

114

I think it is easier to use pgrep

$ pgrep bluetoothd
441

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}'
441
1906
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 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` – gahlot.jaggs Sep 12 '13 at 13:37
  • I tried your solution and it's pretty working but failing at a point where the process name is having whitespaces like below ps -eaf | awk '$NF=="sshd: jaggsmca325@pts/53" {print $2}' any pointer to that problem? And yea I want to get match exact process name. – gahlot.jaggs Sep 13 '13 at 05:03
  • @gahlot.jaggs As I used `$NF`, you can also use `$(NF-1)`, etc. So you could use `$(NF-1)=="sshd:" && $NF=="jaggsmca325@pts/53"` – fedorqui Sep 13 '13 at 10:01
  • Thanks for your all efforts, but I can not know at runtime how many tokens I would be having that time so I can not use $(NF-1) as otherwise it will be fixed to N-1 only and will fail for other cases. Is there any way I could find the process id by using process name, rather than using ps -eaf – gahlot.jaggs Sep 13 '13 at 11:02
  • @gahlot.jaggs You can also `grep "process$"`, as `$` means end of line. Then give the string to awk to print the 2nd record. – fedorqui Sep 13 '13 at 11:03
  • `pgrep` works on Ubuntu 14.04. A point to note if you are feeding the output into a script is that if multiple processes share the name, it will print a list of pids, one per line. – ahcox Feb 09 '15 at 12:35
  • This isn't a good solution, and it requires an exact match of what's almost always a very long command that's impractical to enter. A partial matching solution would be far more useful. – Cerin Jun 29 '21 at 02:31
  • @Cerin yep, agreed. That's why I offer some alternatives. – fedorqui Jun 29 '21 at 07:39
13

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 :)

tripleee
  • 175,061
  • 34
  • 275
  • 318
k-messaoudi
  • 347
  • 2
  • 5
1

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 }'
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1
ps -C <the-name> -o etime=

My ps is from procps-ng .

numeric
  • 475
  • 3
  • 15