问题
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