Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 784
andig Avatar asked Apr 22 '15 14:04

andig


People also ask

How do I run a background task in Python?

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.


2 Answers

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
like image 181
andig Avatar answered Sep 20 '22 14:09

andig


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.

like image 24
xmonk Avatar answered Sep 22 '22 14:09

xmonk