15

I have a script that looks like this:

pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
popd

The selenium servers needs to be running for the tests, however after the phpunit command finishes I'd like to kill the selenium-server that was running.

How can I do this?

7
  • @isalgueiro what if their are other processes running in the background? May 11, 2015 at 15:01
  • %1 stands for "first background job", and your script only sends one process to background tldp.org/LDP/abs/html/x9644.html
    – isalgueiro
    May 11, 2015 at 15:03
  • @isalgueiro yeah but what if someone who is running the script has a background process already running in the terminal before running this script? May 11, 2015 at 15:06
  • I don't think the script runs on the same shell instance. Anyway, if you don't want to rely on job number (i.e. %1) you can save $! after running nohup and kill the process by PID.
    – isalgueiro
    May 11, 2015 at 15:09
  • 2
    Then the best option would be run kill %- which will kill the last job May 11, 2015 at 15:09

3 Answers 3

38
+50

You can probably save the PID of the process in a variable, then use the kill command to kill it.

pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
serverPID=$!
cd web/code/protected/tests/
phpunit functional/
kill $serverPID
popd

I haven't tested it myself, I'd like to write it on a comment, but not enough reputation yet :)

0
5

When the script is excecuted a new shell instance is created. Which means that the jobs in the new script would not list any jobs running in the parent shell.

Since the selenium-server server is the only background process that is created in the new script it can be killed using

#The first job 
kill %1

Or

#The last job Same as the first one
kill %-
2

As long as you don't launch any other process in the background - which you don't - you can use $! directly:

pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
kill $!
popd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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