Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Signals from the supervisor are not propagated to the child processes
- set -eu
- if [[ $# -eq 0 ]] ; then
- echo "Usage $0 <pid_file> <command> [with arguments]"
- exit 0
- fi
- pidfile=$1
- command="${@:2}"
- # Proxy signals
- function trap_sigterm(){
- kill -SIGTERM $(cat $pidfile)
- sleep_while_process_is_alive 0.1
- exit 0
- }
- trap trap_sigterm SIGTERM SIGINT
- function trap_sigusr2() {
- kill -SIGUSR2 $(cat $pidfile)
- }
- trap trap_sigusr2 SIGUSR2
- function sleep_while_process_is_alive() {
- while [ -f $pidfile ] ; do
- PID=$(cat $pidfile)
- if [ -n "$PID" -a -e /proc/$PID ]; then
- sleep $1
- else
- return
- fi
- done
- }
- # Run command in background
- $command &
- # Wait until pid is created
- sleep 2
- # Loop while the pidfile and the process exist
- sleep_while_process_is_alive 0.5
- # Exit unexpected
- exit 1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement