150

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

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

0

6 Answers 6

292

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

pgrep -f keyword

From the man page:

-f       The pattern is normally only matched against the process name. When -f is set, 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. That's a useful trick to avoid matching the awk command itself.

5
  • 5
    ps is overused, and pgrep so underused. Thanks for the post. Nov 25, 2013 at 4:23
  • 14
    One way to pass the output to kill is: kill -9 `pgrep -f keyword`
    – Kris
    Jan 19, 2017 at 10:11
  • This answer is the best ever. So much time I've wasted with ps aux | grep chrome
    – Brandon
    Feb 27, 2017 at 14:23
  • Had to use the [k] trick on pgrep -f. My script was running in a subshell so I think it was picking up its parent command (hard to know for sure - the pid it returned was gone when the command was done executing!) Jul 10, 2018 at 19:17
  • 5
    @Kris for this use case wouldn't be more straightforward to just use pkill -9 -f keyword?
    – oidualc
    Oct 4, 2019 at 7:10
68

Try

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

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

2
  • 3
    Because this can return more than one pid you can get the first by appending | head -1.
    – Kris
    Jan 19, 2017 at 10:29
  • 'head -1' will return grep PID in some linux, Should be tail -1. Dec 31, 2018 at 12:00
28

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

3
  • 4
    Perhaps a typo? ps -ef | grep KEYWORD | grep -v grep | awk '{print $2}'
    – rmv
    Apr 24, 2014 at 7:03
  • Do you know how to pass the returned PID into "kill -9" ?!
    – alybadawy
    Aug 17, 2016 at 1:26
  • 9
    @alybadawy just add | xargs kill -9 to the end
    – patte
    Feb 5, 2017 at 20:45
11

This is available on linux: pidof keyword

10

I use

ps -C "keyword" -o pid=

This command should give you a PID number.

10

To kill a process by a specific keyword you could create an alias in ~/.bashrc (linux) or ~/.bash_profile (mac).

alias killps="kill -9 `ps -ef | grep '[k]eyword' | awk '{print $2}'`"
1
  • 1
    Perfect! Adding a pipe to the end of | head -1 wraps this all up nice and neat.
    – Russ
    May 7, 2019 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.