Trap handler

This commit is contained in:
Kroese 2023-05-08 22:17:21 +02:00 committed by GitHub
parent d6d955f3b1
commit 398b68fd5b

View File

@ -1,13 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eu set -eu
trap 'pkill -f nc || true' EXIT stop() {
trap exit SIGINT SIGTERM trap - SIGINT EXIT
{ pkill -f nc || true } 2>/dev/null
}
trap 'stop' EXIT SIGINT SIGTERM SIGHUP
if [[ "$2" == "/"* ]]; then if [[ "$2" == "/"* ]]; then
while true ; do while true ; do
nc -lp "${1:-5000}" -e "$2" >/dev/null 2>&1 & wait $! nc -lp "${1:-5000}" -e "$2" & wait $!
done done
else else
@ -19,7 +23,7 @@ else
RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n" RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n"
while true; do while true; do
echo -en "$RESPONSE" | nc -lp "${1:-5000}" >/dev/null 2>&1 & wait $! echo -en "$RESPONSE" | nc -lp "${1:-5000}" & wait $!
done done
fi fi