0

I am trying to write an init.d script, I am trying to kill a named node process:

kill $(ps aux | grep 'eproxy' | awk '{print $2}')

The problem is, when running just the ps command on it's own, I am returned TWO process IDs, and not one as expected. ('eproxy' is the name of the node process I want to kill.)

The first process returned is the one I want to kill, but the second process is strange. When I run ps -a | grep PID replacing PID with the second mystery PID, I get a grep process:

20929 pts/1    S+     0:00 grep --color=auto 20921

My question is, how is grep matching that second process to my query? From what I can see, the grep process is not named eproxy so I am unsure why it's being returned along with the proper PID.

  • The process created for your grep contains the text of whatever you're grepping for. In this case, the PID or eproxy. So it is returned as well. The simple solution is to pipe it into a second grep that filters out grep. e.g. ps -a | grep PID | grep -v grep. – Natolio Jun 23 '22 at 18:56
  • 1
    @Natolio even simpler use pgrep which automatically excludes itself (and is noted in the dupe) or for this case pkill which does the whole job correctly with no effort – dave_thompson_085 Jun 24 '22 at 01:53

0 Answers0