Skip to content

Instantly share code, notes, and snippets.

@edgar
Created March 1, 2018 14:40
Show Gist options
  • Save edgar/3c70f7fabc027263fb23c8075c7f1608 to your computer and use it in GitHub Desktop.
Save edgar/3c70f7fabc027263fb23c8075c7f1608 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Trap sigterm and sleep before passing signal to child for $SIGNAL_TIMEOUT seconds
# this is to address a shutdown issue with traefik: https://docs.traefik.io/user-guide/marathon/#shutdown
cmd=${*}
# default to 10 seconds
SIGNAL_TIMEOUT=${SIGNAL_TIMEOUT:-10}
log() {
echo "[$(date +%s)] run.sh -- ${1}"
}
trap_term() {
log "Received sigterm, sleeping for: ${SIGNAL_TIMEOUT}s"
sleep ${SIGNAL_TIMEOUT}s
log "Forwarding sigterm to: ${child_pid}"
kill -15 ${child_pid}
# block returning until children are dead
wait ${child_pid}
log "child pid has finished.. exiting"
}
trap trap_term SIGTERM
log "About to run: ${cmd}"
${cmd} &
child_pid=$!
log "Child PID: ${child_pid}"
wait ${child_pid}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment