Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?)

share|improve this question
add comment

5 Answers 5

up vote 7 down vote accepted

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}'`
share|improve this answer
    
pkill doesn't work on my server for this, but your improved grep strip helps a lot. –  Eric Mar 2 '12 at 3:57
add comment

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
share|improve this answer
    
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 Feb 29 '12 at 4:21
1  
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 Feb 29 '12 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). –  Patrick Feb 29 '12 at 5:16
add comment

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

share|improve this answer
    
This is a handy trick, indeed. Why does it work? –  Herman Torjussen Feb 29 '12 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 Feb 29 '12 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 Jun 17 '12 at 21:15
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
1  
You cannot be sure that the selenium process is using a lower PID. –  FJRA Mar 5 '12 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. –  SkyDan Mar 5 '12 at 12:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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