15

I am trying to use pidof or pgrep to be able to send a HUP to a process in my system. The problem is that I only want to kill the process with a precise parameter.

This is the output of 'ps awx'

  657 ?        S      0:00 processname software
  658 ?        S      0:00 processname demo
  659 ?        S      0:00 processname test

By doing one of these:

pidof processname 
pgrep processname

You get the list of all the processes starting by processname, but i'd like to do something like:

pidof processname test
pgrep processname test

To retrieve only the PID I need (in this example would be 659)

UPDATE

By using the -f flag on pgrep just does what I wanted, by doing:

pgrep -f "processname test"

You'll get the right answer.

aseques
  • 718
  • 4
  • 12
  • 28
  • Right, you generally need `-f` because you want to match on the full command line. Note that pgrep operates on regular expressions, so you can do relatively complex matching, also. – cjc May 15 '12 at 10:00

1 Answers1

11
$ ps aux |grep [d]evio
user01   10220  0.0  0.1   5376  2424 pts/5    S+   11:41   0:00 ssh devio
$ pgrep -f "ssh devio"
10220
$ pkill -0 -f "ssh devio" ; echo $?
0
jirib
  • 1,240
  • 8
  • 15