How can I start and stop a background task on travis?

浪子不回头ぞ 提交于 2021-02-19 01:35:36

问题


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

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

However, SERVER_PID is empty.

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)?


回答1:


The following should work:

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

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.




回答2:


Answering question here as @xmonk's answer is correct in terms of functionality but requires an external shell script which- in turn- would need to use a temp file to write the pid value to.

I've just found out that travis-ci does actually allow multi-line statements which simplified the whole thing. Put this in .travis.yml:

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


来源:https://stackoverflow.com/questions/29800968/how-can-i-start-and-stop-a-background-task-on-travis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!