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)?
We can configure a new daemon thread to execute a custom function that will perform a long-running task, such as monitor a resource or data. For example we might define a new function named background_task(). Then, we can configure a new threading. Thread instance to execute this function via the “target” argument.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With