After my bash script is done with the selenium server, the script kills it like this:

kill `ps -ef | grep selenium | awk '{ print $2 }'`

While this works and it does kill the selenium script, it also tries to kill the process for grep selenium So by the time that process number gets back to the kill command, the process is gone, so it ends up generating an error message. (In other words: it ends up killing two processes, one of them is the real selenium server, the other is a fleeting process that is a side-effect of how I'm doing this command.)

Is there a more graceful way to kill the process, that doesn't have this unintended side-effect?

(FWIW one idea I had: this same script starts up selenium earlier in the script, so maybe if there was a way I could capture the PID when I start up selenium, I could just keep that and kill it directly, instead of grepping for the process ID. The problem is, I don't know how to get that PID, how to use a variable to keep it and how to reference it later in the script. But is that a better way to go about it?)

5 Answers 5


Try the pgrep command, which will output the PID of the command you're interested in.

pgrep selenium

To actually kill the process, use the companion pkill command.

pkill selenium
  • I love the elegance of this, but the process looks like this: "root 26401 11997 2 23:20 pts/1 00:00:00 java -jar /home/lm/cron/selenium-server-standalone-2.19.0.jar" so I just changed it to pgrep java and it works :-) thanks!
    – Eric
    Commented Feb 29, 2012 at 4:21
  • 2
    If you give pgrep the '-f' flag, it will match the pattern against the entire command-line. So, in this case kill pgrep -f selenium should work
    – mills013
    Commented Feb 29, 2012 at 4:31
  • 7
    using pkill is a lot simpler. pkill uses the exact same matching criteria as pgrep, so you can do this in 1 command instead of 2 (and having to rely on the output of one).
    – phemmer
    Commented Feb 29, 2012 at 5:16

Try to use pkill selenium if neither this, nor one of the others work you can keep using your method with another pipe sequence such as

kill `ps -ef|grep -i selenium| grep -v grep| awk '{print $2}'`
  • pkill doesn't work on my server for this, but your improved grep strip helps a lot.
    – Eric
    Commented Mar 2, 2012 at 3:57

Try using grep [s]elenium. This will not find the grep process.

  • This is a handy trick, indeed. Why does it work?
    – user13742
    Commented Feb 29, 2012 at 11:38
  • 2
    @hesse - grep uses BREs by default. That is, [ and ] show up in argv for grep, but it doesn't match because [ and ] aren't being used literally by grep.
    – Chris Down
    Commented Feb 29, 2012 at 20:44
  • I've often used a | grep -v grep component in the pipe. But this is pretty elegant and I like it!
    – Alexios
    Commented Jun 17, 2012 at 21:15
  • This is (often) not enough if you have a file in the current working directory with the same name as the process you're grepping for. So, the solution I normally use is: ps -ef | grep \[s]elenium. Though I found lsof to be a much better alternative to using ps and grep.
    – ikaerom
    Commented Jun 3, 2017 at 14:00

The killall command hasn't been mentioned, and I feel it is a useful addition to the repertoire.

$ killall selenium

You can also use the -r regex option to specify a regular expression to match the process name, and even a time value:

$ killall -o 5m selenium 

Kills all selenium processes older than 5 minutes.

  • That seems to be unique to non-RHEL/CentOS versions of killall
    – ewwhite
    Commented Oct 22, 2014 at 14:01

Use the -m 1 switch with the grep command to fetch only the first found process. As long as you sort your processes by PID, ascending, it will work.

  • 1
    You cannot be sure that the selenium process is using a lower PID.
    – FJRA
    Commented Mar 5, 2012 at 3:58
  • FJRA, a process started earlier always gets lower PID. Because the selenium server is started before the 'grep selenium', it will get a lower PID. Commented Mar 5, 2012 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .