Last active
February 8, 2018 19:10
-
-
Save Mikulas/f6ce24b55ff69f7a1715cac7aee47911 to your computer and use it in GitHub Desktop.
Bash process control
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 | |
set -euo pipefail | |
IFS=$'\n\t' | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
source /tmp/x | |
function handler() { | |
d " child: caught SIGTERM signal, exiting" | |
exit 0 | |
} | |
trap handler SIGTERM | |
d " child: start $$" | |
for N in $(seq 1 5); do | |
d "child: $N" | |
sleep 30 & | |
wait $! | |
done | |
d " child: done" |
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 | |
set -euo pipefail | |
IFS=$'\n\t' | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
source /tmp/x | |
bash /tmp/sig.sh & | |
PID="$!" | |
d "master start" | |
sleep 1 | |
d "master: sending SIGTERM to $PID" | |
kill -s TERM $PID | |
wait "$PID" |
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 | |
set -euo pipefail | |
IFS=$'\n\t' | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
source /tmp/x | |
child=0 | |
function handler() { | |
d a" sig: caught SIGTERM signal" | |
d a" sig: forwarding SIGTERM to $child" | |
kill -s TERM "$child" | |
d a" sig: waiting for process $child" | |
wait "$child" | |
d a" sig: process $child exited, exiting this wrapper" | |
exit 0 | |
} | |
trap handler SIGTERM | |
d a" sig: start $$" | |
while true; do | |
bash /tmp/child.sh & | |
child=$! | |
wait $child | |
sleep 10 | |
done |
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
function d() { | |
NOW="$(php -r 'echo microtime(TRUE);')" | |
echo "$NOW $@" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Example output: