From 476486195c8da6718e934bb0e389275e900d4f58 Mon Sep 17 00:00:00 2001 From: Nicolas Van Wambeke Date: Sun, 23 Apr 2023 11:25:38 +0200 Subject: [PATCH] added a trap for SIGINT to gracefully kill components upon CTRL+C --- tsock_video | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tsock_video b/tsock_video index 320bb83..cf09cce 100755 --- a/tsock_video +++ b/tsock_video @@ -4,9 +4,29 @@ puits=false sourc=false protocol="tcp" port=`expr \`id -u\` % 3000 + 14578` +interrupted=1 usage() { echo "Usage: $0 [[-p|-s] [-t (tcp|mictcp)]" 1>&2; exit 1; } +# Stop all components gracefully +function graceful_stop () +{ + + echo "Arret" + killall -9 cvlc > /dev/null 2>&1 + killall -9 vlc > /dev/null 2>&1 + killall -9 gateway > /dev/null 2>&1 + + if [ $interrupted -eq 1 ]; then + # Set return code to 2 + exit 2 + fi +} + +# initialise trap to call graceful_stop function +# when signal 2 (SIGINT) is received +trap "graceful_stop" 2 + while getopts "pst:" o; do case "${o}" in t) @@ -55,9 +75,5 @@ fi echo "Appuyez sur ENTREE pour arreter" read line - -echo "Arret" -killall -9 cvlc > /dev/null 2>&1 -killall -9 vlc > /dev/null 2>&1 -killall -9 gateway > /dev/null 2>&1 - +interrupted=0 +graceful_stop