2015-05-11 31 views
4

我有一个脚本,看起来像这样:开始在后台过程中,做一个任务,然后杀死进程在后台

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

服务器需要运行的测试的硒,但后phpunit命令完成我想杀死正在运行的硒服务器。

我该怎么做?

+0

也许'kill%1'? – isalgueiro

+0

@isalgueiro如果他们是在后台运行的其他进程呢? – ComputerLocus

+0

'%1'代表“第一个后台作业”,你的脚本只发送一个进程到后台http://www.tldp.org/LDP/abs/html/x9644.html – isalgueiro

回答

3

当脚本excecuted一个新的shell实例被创建。这意味着新脚本中的jobs不会列出在父shell中运行的任何作业。

由于selenium-server服务器是在新的脚本创建它可以使用

#The first job 
kill %1 

或者

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

您可以将进程的PID保存在一个变量中,然后使用kill命令杀死它。

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

我没有测试过我自己,我希望把它写在评论,但没有足够的声誉还没有:)

+1

这确实有用! – ComputerLocus

0

只要你不启动任何其他被杀死的唯一后台进程在后台进程 - 你不用 - 你可以使用$!直接:

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