94

In order to search for a process you can use ps with grep.

For example to search for firefox

ps aux | grep firefox

How to get the same answer without using grep?

2
  • 1
    I'm curious why you would want to do this, given that ps/grep solution works so well?
    – neuronet
    Nov 4, 2018 at 15:36
  • 1
    OP just wants a shorter command I'd guess. If there would be a thing like ps -n <process name> that would answer his need. ps -n firefox is a bit shorter than ps | grep firefox. ps can already filter on pid or processes for a user id, so it's a reasonable question to filter on process name. Nov 7, 2018 at 9:04

8 Answers 8

102

The pgrep command, and its sibling pkill, exists precisely for this purpose:

  • pgrep firefox will list all processes whose commands match firefox
  • pgrep -f firefox will list all processes whose entire command lines match firefox
  • pgrep -x firefox will list all processes whose commands exactly match firefox
  • ... and so on.

And naturally, pgrep will exclude itself from the match, so none of the grep rituals associated with ps | grep are needed.


The other set of tools for this are the pidof and killall commands. These aren't as flexible as pgrep and pkill.

  • pidof firefox will list processes whose command is firefox
1
  • Useful if you have a long command name and/or command line args is pgrep -f foo which uses the full process name to match foo
    – Charlie G
    Oct 7, 2022 at 16:10
36
ps -fC process-name

example:

ps -fC firefox

from man ps

  -C cmdlist      Select by command name.
                       This selects the processes whose executable name is
                       given in cmdlist.


 -f              Do full-format listing. This option can be combined
                       with many other UNIX-style options to add additional
                       columns. It also causes the command arguments to be
                       printed. When used with -L, the NLWP (number of
                       threads) and LWP (thread ID) columns will be added. See
                       the c option, the format keyword args, and the format
                       keyword comm.
1
  • 5
    This is the best answer but unfortunately doesn't work on OSX. The BSD ps -C flag behaves completely differently - "Change the way the CPU percentage is calculated" Jan 9, 2018 at 17:28
5

A cool trick

$ps -ejH

You will get all the processes with names

exmple:
1747   568   568 ?        00:00:00   colord
1833  1832  1832 ?        00:00:00   gnome-keyring-d
2263   568   568 ?        00:00:00   udisksd
2311  2311  2311 ?        00:00:00   cupsd
2315  2315  2311 ?        00:00:00     dbus

Redirect or so copy the output to a file and then open nano, press Ctrl+W and you can search for the name you want.

3

top allows you to search for string when you hit uppercase L; the process will be highlighted, and use up and down arrow keys to scroll through list of processes. Similarly, htop command allows highlighting a particular process when you hit /. And \ will filter all the processes with a particular string in the name.

For those who like awk, here's an awk oneliner: ps -eF | awk '/process-name/ {print $11}' . With ps -eF process name is always in 11th column. Alternatively if you do ps -eF | awk '{print $11}' | sort you get a sorted list of processes names, sorted alphabetically. Pipe it into less command just to view the long list of files easier.

2

You can also use htop and then hit F4 to filter the results with a matching user-defined string. You also have a custom search feature available by hitting F3.

2

If two processes is the problem, you can use only grep:

grep firefox /proc/*/cmdline
2

If the reason you don't want to use ps | grep is because it loses the first line (the column headers), you can do:

ps aux | grep 'firefox\|^USER'

This is grepping for a line that contains firefox or a line that starts with USER (the first line of header line on my distro).

0

I just read this ps alias on the Lennart Poettering Blog. The output is according to the systemd control group parenting:

alias psc='ps xawf -eo pid,user,cgroup,args'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .