Skip to content

Instantly share code, notes, and snippets.

@xynova
Last active August 4, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xynova/b04560de7e7ab63d95253406883f9c13 to your computer and use it in GitHub Desktop.
Save xynova/b04560de7e7ab63d95253406883f9c13 to your computer and use it in GitHub Desktop.
nginx+confd startup scripts for docker container
#!/bin/sh
set -ebm
syncronize_bakends() {
echo "[nginx-confd] Synchronizing with confd backends"
# Loop until confd has updated the nginx config
until confd --onetime --config-file /etc/confd/confd.toml --log-level error -sync-only; do
echo "[nginx-confd] waiting for confd to refresh nginx configuration"
sleep 5
done
}
start_confd() {
echo "[nginx-confd] Starting confd process"
# Start process
confd --config-file /etc/confd/confd.toml &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start confd: $status"
exit $status
fi
}
start_nginx(){
echo "[nginx-confd] Starting nginx process"
# Start process
nginx -g "daemon off;" &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start nginx: $status"
exit $status
fi
}
supervise(){
local confd_process_status
local nginx_process_status
echo "[nginx-confd] Enter supervision mode"
sleep 2 # give time for services to start
while /bin/true; do
ps aux | grep 'confd --config-file' | grep -q -v grep
confd_process_status=$?
ps aux | grep 'master process nginx' | grep -q -v grep
nginx_process_status=$?
# If the greps above find anything, they will exit with 0 status
# If they are not both 0, then something is wrong
if [ $confd_process_status -ne 0 -o $nginx_process_status -ne 0 ]; then
echo "One of the processes has already exited."
exit -1
fi
sleep 10
done
}
cleanup() {
# An even simpler way to kill all child process of a bash script
pkill -P $$
}
trap cleanup EXIT
syncronize_bakends
start_confd
start_nginx
supervise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment