Updated whole structure of subproccesses (added traps to manage signals KILL/INT) + (added mini-log)

This commit is contained in:
xdrm-brackets 2018-01-28 13:44:55 +01:00
parent 8ff5997467
commit 08f30d2996
2 changed files with 36 additions and 6 deletions

38
launch
View File

@ -39,10 +39,14 @@ echo -ne "" > $BUF_OUT;
while [ $infail -lt 10 ]; do while [ $infail -lt 10 ]; do
# main command # 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 # 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}'`; LISTEN_PID=`lsof -n -i4TCP:$4 | grep LISTEN | awk '{print $2}'`;
test -z "$LISTEN_PID" && kill -9 $LISTEN_PID; test -z "$LISTEN_PID" && kill -9 $LISTEN_PID;
fi; fi;
@ -61,7 +65,7 @@ IN_PID=$!;
################################ ################################
# IF SET, SEND TRIGGER TO PEER # # 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 # # WAIT 2s FOR THE PEER TO LISTEN #
@ -79,12 +83,14 @@ sleep 2;
outfail=0; outfail=0;
# stop after 20 failed attempts # stop after 5 failed attempts
while [ $outfail -lt 20 ]; do while [ $outfail -lt 5 ]; do
# main command # 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 sleep 1; # can sleep because the buffer is never flushed
done& done&
@ -100,3 +106,23 @@ OUT_PID=$!;
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Store config file in 'config_$NAME' # (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; 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;

View File

@ -7,4 +7,8 @@ ROOT=$(dirname `realpath $0`);
test $# -lt 1 && echo "ERR: Missing arguments (trigger_port)" && exit 1; test $# -lt 1 && echo "ERR: Missing arguments (trigger_port)" && exit 1;
# (2) Wait for triggering by peer # (2) Wait for triggering by peer
echo "(.) set up [trigger:$1]";
nc -lp $1; nc -lp $1;
# (3) Log trigger received #
echo " > RECEIVED [trigger:$1]";