Created
July 24, 2018 08:56
-
-
Save nevmerzhitsky/a0f6ade110d5ec3988558374bc699265 to your computer and use it in GitHub Desktop.
Run a main process in docker container
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
#!/usr/bin/env bash | |
# Break script on any non-zero status of any command | |
set -e | |
export DOCKER_CMD_SLEEP | |
TASK_PID=0 | |
term_handler() { | |
if ps -p $TASK_PID > /dev/null; then | |
kill -SIGTERM "$TASK_PID" | |
# Wait returns status of the killed process and with set -e this breaks the script | |
wait "$TASK_PID" || true | |
fi | |
# SIGTERM comes from docker stop so treat it as normal signal | |
exit 0 | |
} | |
trap 'term_handler' SIGTERM | |
echo DOCKER_CMD_SLEEP: $DOCKER_CMD_SLEEP | |
# Periodically executed task | |
while /bin/true; do | |
# Start a main process | |
python main.py & | |
TASK_PID=$! | |
# Sleep in foreground will block traps | |
sleep $DOCKER_CMD_SLEEP & | |
# All currently active child processes (both task and sleep) are waited for, and the return | |
# status is always zero. Thus if the task running longer than sleep amount the wait released | |
# only when the task is finished. | |
wait | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment