Bash – pgrep/pidof usage for complex process names

bashcommand-line-interfaceprocess

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.

Best Answer

$ 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