如何在 travis 上启动和停止后台任务? [英] How can I start and stop a background task on travis?

查看:40
本文介绍了如何在 travis 上启动和停止后台任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 travis 上启动并重新启动自定义 Web 服务器.使用子 shell (.travis.yml) 在后台启动是很好的:

I need to start and restart a custom web server on travis. Starting in background is fine using a sub-shell (.travis.yml):

- if [ "$TEST_ADAPTER" = "HTTP" ]; then (vendor/bin/httpd.php start &); fi

要再次停止/终止进程,我正在尝试获取其 PID,然后将其终止:

To stop/kill the process again I'm trying to get its PID and then kill it:

- if [ "$TEST_ADAPTER" = "HTTP" ]; then (vendor/bin/httpd.php start &) && SERVER_PID=$!; fi
- ...
- if [ "$TEST_ADAPTER" = "HTTP" ]; then kill -9 $SERVER_PID && ...; fi

然而,SERVER_PID 是空的.

在 travis 上获取后台进程的 PID 以停止它的正确方法是什么(复杂性:不使用额外的 shell 脚本)?

What is the right way to obtain the PID of a background process on travis in order to stop it (complication: without using an additional shell script)?

推荐答案

以下应该有效:

if [ "$TEST_ADAPTER" = "HTTP" ]; then
    vendor/bin/httpd.php&
    SERVER_PID=$!
fi

围绕命令的 () 创建一个子 shell.$! 在您的示例中为空,因为程序在子 shell 中运行,但您的 $! 在父 shell 上运行.

The () surrounding the command, creates a sub-shell. The $! comes empty in your examples, because the program is running in the sub-shell, but your $! is running on the parent shell.

这篇关于如何在 travis 上启动和停止后台任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆