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

时间:2021-06-03 19:16:56

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.

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

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


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.

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

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

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

#The first job 
kill %1

Or

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

#2


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

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

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

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

#3


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

#1


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.

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

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

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

#The first job 
kill %1

Or

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

#2


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

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

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

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

#3


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