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

Com*_*cus 6 bash command-line-interface

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

pushd .
nohup java -jar test/selenium-server.jar > /dev/null 2>&1 &
cd web/code/protected/tests/
phpunit functional/
popd
Run Code Online (Sandbox Code Playgroud)

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

我怎样才能做到这一点?

zap*_*lla 20

您可以将进程的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
Run Code Online (Sandbox Code Playgroud)

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


nu1*_*73R 5

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

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

#The first job 
kill %1
Run Code Online (Sandbox Code Playgroud)

或者

#The last job Same as the first one
kill %-
Run Code Online (Sandbox Code Playgroud)