64

I am trying to use the ps -o command to get just specific info about processes matching a certain name. However, I am having some issues on this, when I try to use this even to just get all processes, like so, it just returns a subset of what a normal ps -ef would return (it doesn't return nearly the same number of results so its not returning all running processes)

ps -ef -o pid,time,comm

I want to try something like this (below) but incorporate the ps -o to just get specific info from it (just the PID)

ps -ef |grep `whoami`| grep firefox-bin

Any advice is appreciated as to how to do this properly, thanks

Rick
  • 16,612
  • 34
  • 110
  • 163

5 Answers5

101

This will get you the PID of a process by name:

pidof name

Which you can then plug back in to ps for more detail:

ps -p $(pidof name)
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • 6
    `pgrep` can also be used, but there are some differences in the way they work. For example, on my system running apache2, `pidof` will find "apache2" but not "apache" while `pgrep` will find it either way. – Dennis Williamson Sep 24 '10 at 21:51
  • 5
    OS X people: `brew install pidof` – rogerdpack Oct 11 '16 at 17:49
  • 1
    `pidof` is not standardized by POSIX and is therefore not guaranteed, as evidenced by OS X not having `pidof`. – Wyatt Ward Jan 27 '18 at 19:41
30

This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:

ps -o pid -C bash

EDIT: Dependening on the sort of output you expect, pgrep would be more elegant. This, in my knowledge, is Linux specific and result in similar output as above. For example:

pgrep bash
jlliagre
  • 29,783
  • 6
  • 61
  • 72
h7r
  • 4,944
  • 2
  • 28
  • 31
12
ps -fC PROCESSNAME

ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.

Gerald Hughes
  • 5,771
  • 20
  • 73
  • 131
  • 3
    This doesn't work on mac though. Mac doesn't seem to use `-C` in the same way, do you know how to do this on mac? Thanks! – Noitidart Feb 12 '16 at 07:19
5

Sometimes you need to grep the process by name - in that case:

ps aux | grep simple-scan

Example output:

simple-scan  1090  0.0  0.1   4248  1432 ?        S    Jun11   0:00
user3751385
  • 3,752
  • 2
  • 24
  • 24
2

Sorry, much late to the party, but I'll add here that if you wanted to capture processes with names identical to your search string, you could do

pgrep -x PROCESS_NAME

-x          Require an exact match of the process name, or argument list if -f is given.
             The default is to match any substring.

This is extremely useful if your original process created child processes (possibly zombie when you query) which prefix the original process' name in their own name and you are trying to exclude them from your results. There are many UNIX daemons which do this. My go-to example is ninja-dev-sync.

Spade
  • 2,220
  • 1
  • 19
  • 29