Created
October 21, 2020 18:12
-
-
Save dmolesUC/8b4add6adfe1d09268405e06c48dcb86 to your computer and use it in GitHub Desktop.
Start and terminate multiple sevices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ######################################## | |
# Start server and manager in background | |
/start-server.sh & | |
SERVER_PID=$! | |
/start-manager.sh & | |
MANAGER_PID=$! | |
# ######################################## | |
# Make sure child processes exit cleanly | |
ensure_clean_exit() { | |
trap - SIGINT SIGTERM # clear the trap | |
echo "Killing process $$ and all subprocesses" | |
kill -- -$$ | |
} | |
sigint_received() { | |
echo "SIGINT received" | |
ensure_clean_exit | |
} | |
sigterm_received() { | |
echo "SIGTERM received" | |
ensure_clean_exit | |
} | |
trap sigint_received SIGINT | |
trap sigterm_received SIGTERM | |
# ######################################## | |
# Wait for manager or server to exit | |
wait -n | |
EXIT_STATUS=$? | |
# Whichever exited, kill the other one | |
if ! kill -0 $SERVER_PID 2> /dev/null; then | |
echo "server (PID ${SERVER_PID}) exited with ${EXIT_STATUS}" | |
echo "stopping manager (PID ${MANAGER_PID})" | |
kill -- -$MANAGER_PID 2> /dev/null | |
elif ! kill -0 $MANAGER_PID 2> /dev/null; then | |
echo "manager (PID ${MANAGER_PID}) exited with ${EXIT_STATUS}" | |
echo "stopping server (PID ${SERVER_PID})" | |
kill -- -$SERVER_PID 2> /dev/null | |
fi | |
echo "Exiting with status ${EXIT_STATUS}" | |
exit $EXIT_STATUS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment