3

If I want to check if something is running, I do ps aux | grep flamethrower, expecting just to show the processes containing flamethrower in the name. This will always find at least one process, as grep flamethrower matches this criteria.

Is there some way to check for a specific process, in a cleaner way?

0

1 Answer 1

3

The usual way would be to use pgrep:

$ pgrep init
1
2215
6300
$ ps ax | grep init
    1 ?        Ss     6:41 /sbin/init
 2215 ?        Ss     1:54 init --user --restart --state-fd 26
 6300 ?        S      0:00 init --user --startup-event indicator-services-start
17522 pts/10   S+     0:00 grep --color=auto init

Note that you might need to use other tricks with pgrep as well, if you use it with watch and -f:

$ watch pgrep init -fa
Every 2.0s: pgrep init -fa                                                                                                                                                                                               
Wed Dec  3 19:19:47 2014

1 /sbin/init
2215 init --user --restart --state-fd 26
6300 init --user --startup-event indicator-services-start
18233 watch pgrep init -fa
18234 watch pgrep init -fa
18235 sh -c pgrep init -fa
$ watch pgrep [i]nit -fa
Every 2.0s: pgrep [i]nit -fa                                                                                                                                                                                           Wed Dec  3 19:20:42 2014

1 /sbin/init
2215 init --user --restart --state-fd 26
6300 init --user --startup-event indicator-services-start
8
  • 1
    BTW, congrats on getting to 2k!
    – slm
    Commented Dec 3, 2014 at 13:36
  • I would use ps ax | grep [i]nit (this work for a fixed string of course)
    – Archemar
    Commented Dec 3, 2014 at 13:43
  • @Archemar I need to do that with pgrep too, if use it with watch.
    – muru
    Commented Dec 3, 2014 at 13:47
  • @slm thanks. Looking forward to more involvement here.
    – muru
    Commented Dec 3, 2014 at 13:48
  • But: from pgrep manual: «The process name used for matching is limited to the 15 characters present in the output of /proc/pid/stat. Use the -f option to match against the complete command line, /proc/pid/cmdline.», So, what if I wan't to search on process complete (not-truncated) names (without full path or arguments)?
    – Pablo A
    Commented Mar 6, 2018 at 1:36

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