From 08f30d29965fb90d9037fd4645799d9b6da2c3ef Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 28 Jan 2018 13:44:55 +0100 Subject: [PATCH] Updated whole structure of subproccesses (added traps to manage signals KILL/INT) + (added mini-log) --- launch | 38 ++++++++++++++++++++++++++++++++------ trigger | 4 ++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/launch b/launch index c14a0a0..b76c0a3 100755 --- a/launch +++ b/launch @@ -39,10 +39,14 @@ echo -ne "" > $BUF_OUT; while [ $infail -lt 10 ]; do # main command - nc -lp $4 >> $BUF_IN && infail=0 || infail=`expr $infail + 1`; + echo "(.) started [input]"; + nc -lp $4 >> $BUF_IN 2> /dev/null && infail=0 || ( infail=`expr $infail + 1`; exit 1 ) || echo "Port $4 already in use"; # try to kill proccess that uses port if failed and such proccess exists - if [ $infail -gt 0 ]; then + if [ $infail -eq 0 ]; then + echo " > END [input]"; + else + echo " > FAIL [input]"; LISTEN_PID=`lsof -n -i4TCP:$4 | grep LISTEN | awk '{print $2}'`; test -z "$LISTEN_PID" && kill -9 $LISTEN_PID; fi; @@ -61,7 +65,7 @@ IN_PID=$!; ################################ # IF SET, SEND TRIGGER TO PEER # ################################ -test ! -z "$TRIGGER" && bash -c "exec 3<>/dev/tcp/$2/$TRIGGER"; +test ! -z "$TRIGGER" && ( echo "(.) sending [trigger:$2:$TRIGGER]"; bash -c "exec 3<>/dev/tcp/$2/$TRIGGER" && echo " > SENT [trigger:$2:$TRIGGER]" || echo " > ERR [trigger:$2:$TRIGGER]" ) ################################## # WAIT 2s FOR THE PEER TO LISTEN # @@ -79,12 +83,14 @@ sleep 2; outfail=0; - # stop after 20 failed attempts - while [ $outfail -lt 20 ]; do + # stop after 5 failed attempts + while [ $outfail -lt 5 ]; do # main command - tail -f $BUF_OUT | nc $2 $3 && outfail=0 || outfail=`expr $outfail + 1`; + echo "(.) started [output]"; + tail -f $BUF_OUT | nc $2 $3 2>/dev/null && outfail=0 || ( outfail=`expr $outfail + 1`; exit 1 ) || echo "Cannot connect to $2:$3"; + test $outfail -eq 0 && echo " > END [output]" || echo " > FAIL [output]"; sleep 1; # can sleep because the buffer is never flushed done& @@ -100,3 +106,23 @@ OUT_PID=$!; #--------------------------------------------------------# # (1) Store config file in 'config_$NAME' echo -e "#!/bin/bash\nBUF_IN=\"$BUF_IN\"; BUF_OUT=\"$BUF_OUT\";\nIN_PID=$IN_PID; OUT_PID=$OUT_PID;\n" > $ROOT/config_$1; + +# (2) Notify server started # + + + +# (6) Manage ending proccess +#--------------------------------------------------------# +# (1) Kill subproccesses if Ctrl+C # +trap "kill -9 $OUT_PID 2>/dev/null; kill -9 $IN_PIT 2>/dev/null; rm $ROOT/config_$1; pkill nc 2>/dev/null;" INT; +trap "kill -9 $OUT_PID 2>/dev/null; kill -9 $IN_PIT 2>/dev/null; rm $ROOT/config_$1; pkill nc 2>/dev/null;" KILL; + +# (2) Wait for daemons to stop # +wait $OUT_PID; +wait $IN_PID; +pkill nc 2>/dev/null; + +# (3) Remove config file # +rm $ROOT/config_$1; + + diff --git a/trigger b/trigger index c4ff82b..f9b8cd3 100755 --- a/trigger +++ b/trigger @@ -7,4 +7,8 @@ ROOT=$(dirname `realpath $0`); test $# -lt 1 && echo "ERR: Missing arguments (trigger_port)" && exit 1; # (2) Wait for triggering by peer +echo "(.) set up [trigger:$1]"; nc -lp $1; + +# (3) Log trigger received # +echo " > RECEIVED [trigger:$1]";