Receiving messages from ps -ef | grep keyword

I want to use ps -ef | grep "keyword" ps -ef | grep "keyword" to determine the pid of the daemon process (it has a unique ps -ef output line).

I can kill the process with pkill keyword is there any command that returns pid instead of killing it? (pidof or pgrep does not work)

+80
linux shell daemon
Nov 14 2018-11-11T00:
source share
5 answers

You can use pgrep as long as you include the -f options. This makes pgrep match keywords in the entire command (including arguments) instead of the process name.

 pgrep -f keyword 

On the man page:

-f Typically, a pattern is mapped only to a process name. When -f installed, the full command line is used.




If you really want to avoid pgrep, try:

 ps -ef | awk '/[k]eyword/{print $2}' 

Note the [] around the first letter of the keyword. This is a useful trick to avoid matching the awk command itself.

+167
Nov 14 '11 at 10:41
source share

Try

ps -ef | grep "KEYWORD" | awk '{print $2}'

This command should give you the PID of the processes with the KEYWORD in them. In this case, awk returns what is in the 2nd column from the output.

+38
Nov 14 '11 at 10:37
source share

ps -ef | grep KEYWORD | grep -v grep | awk '{print $2}'

+8
Feb 03 '13 at 4:35 am
source share

I use

 ps -C "keyword" -o pid= 

This command should give you the PID number.

+6
Mar 14 '16 at 17:50
source share

It is available on linux: pidof keyword

+5
Nov 14 '11 at 10:44
source share



All Articles