I have a test suite that queries a localhost-based server. The server process needs to be running in order for the server to work. The process may be running already for debugging work, but it might not be. If a server process is running, it's probably safe to assume it's the correct one. If we started the process, we should kill it when we're done, but if it was already running, we should leave it.
I've written a bash script which seems to work, but isn't quite perfect.
if [ -z `pgrep -x server-process` ]; then
mkdir ./data
server-process ./data &
sleep 1; # Wait for server to print output,
# reporting initialization details etc.
fi
# Set up environment variables...
run-test-suite $@
if jobs %1 2>/dev/null; then
kill %1
rm -R ./data;
fi
In particular:
- Is there a more robust way to do this?
- Are there any edge cases I've missed?
- Is there a better way to wait for
server-process
's initial output, rather thansleep 1
(which is often not long enough anyway)?server-process
doesn't close the pipe - it could still report errors - I don't mind if these are interleaved with test suite output. I just want to wait until after the server has printed it's initial config information.