bigupd: launch (1) updated trap system-simplified (2) fixed input server netcat kill (3) minmod for comments and structure (4) added traps from within bg-proccesses (input+output) + netcat call set in background so to be killed by these traps -> trap recursivity

This commit is contained in:
xdrm-brackets 2018-01-29 15:29:14 +01:00
parent 1893a5bd49
commit e4fabe8853
1 changed files with 38 additions and 14 deletions

52
launch
View File

@ -37,6 +37,8 @@ echo -ne "" > $BUF_OUT;
# (1) Start the script in background # (1) Start the script in background
( (
trap "echo 'launch.server.killed'; kill -9 %1 2>/dev/null && echo 'launch.server.nc.killed';" INT KILL;
infail=0; infail=0;
# kill script after 10 failures # kill script after 10 failures
@ -44,7 +46,8 @@ echo -ne "" > $BUF_OUT;
# main command # main command
echo "(.) started [input]"; 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"; ( nc -lp $4 >> $BUF_IN 2> /dev/null && infail=0 || ( infail=`expr $infail + 1`; exit 1 ) || echo "Port $4 already in use" ) &
wait $!;
# 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 -eq 0 ]; then if [ $infail -eq 0 ]; then
@ -52,7 +55,7 @@ echo -ne "" > $BUF_OUT;
else else
echo " > FAIL [input]"; 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 2>/dev/null;
fi; fi;
done; done;
@ -80,11 +83,13 @@ sleep 2;
# (4) Launch OUTPUT server # (4) Launch OUTPUT client
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Launch output server # (1) Start the script in background
( (
trap "echo 'launch.client.killed'; kill -9 %1 2>/dev/null && echo 'launch.client.nc.killed';" INT KILL;
outfail=0; outfail=0;
# stop after 5 failed attempts # stop after 5 failed attempts
@ -92,9 +97,11 @@ sleep 2;
# main command # main command
echo "(.) started [output]"; 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"; ( tail -f $BUF_OUT | nc $2 $3 2>/dev/null && outfail=0 || ( outfail=`expr $outfail + 1`; exit 1 ) || echo "Cannot connect to $2:$3" ) &
wait $!;
test $outfail -eq 0 && echo " > END [output]" || echo " > FAIL [output]"; 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;
@ -118,18 +125,35 @@ echo -e "#!/bin/bash\nBUF_IN=\"$BUF_IN\"; BUF_OUT=\"$BUF_OUT\";\nIN_PID=$IN_PID;
# (6) Manage ending proccess # (6) Manage ending proccess
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Kill subproccesses if Ctrl+C # # (1) Kill subproccesses if Ctrl+C #
trap "kill -9 $OUT_PID 2>/dev/null; kill -9 $IN_PIT 2>/dev/null; rm $CONF_FILE; pkill nc 2>/dev/null; rm $BUF_OUT; rm $BUF_IN; exit 1;" INT; on_exit(){
trap "kill -9 $OUT_PID 2>/dev/null; kill -9 $IN_PIT 2>/dev/null; rm $CONF_FILE; pkill nc 2>/dev/null; rm $BUF_OUT; rm $BUF_IN; exit 1;" KILL; echo "launch.kill";
## {1} Kill client ##
echo 'launch.client.kill';
kill -9 $OUT_PID 2>/dev/null;
## {2} Kill server ##
echo 'launch.server.kill';
kill -9 $IN_PID 2>/dev/null;
## {3} Remove config file ##
echo 'launch.config.delete';
rm $CONF_FILE 2>/dev/null;
## {4} Remove buffers ##
echo 'launch.buffer.in.delete';
echo 'launch.buffer.out.delete';
rm $BUF_OUT $BUF_IN 2>/dev/null;
exit 0;
}
# (2) Setup trap for signals INT + KILL #
trap "on_exit" INT KILL;
# (2) Wait for daemons to stop # # (2) Wait for daemons to stop #
wait $OUT_PID; wait $OUT_PID;
wait $IN_PID; wait $IN_PID;
pkill nc 2>/dev/null;
# (3) Remove config file # on_exit;
rm $CONF_FILE;
# (4) Remove buffers #
rm $BUF_OUT;
rm $BUF_IN;