在后台启动进程,执行任务,然后在后台终止进程

问题描述 投票:5回答:3

我有一个看起来像这样的脚本:

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

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

我怎样才能做到这一点?

bash command-line-interface
3个回答
4
投票

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

由于selenium-server服务器是在新脚本中创建的唯一后台进程,因此可以使用它来终止它

#The first job 
kill %1

要么

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

19
投票

您可以将进程的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

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


0
投票

只要你不在后台启动任何其他进程 - 你没有 - 你可以使用$!直:

pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
kill $!
popd
© www.soinside.com 2019 - 2024. All rights reserved.