diff --git a/sats/main b/sats/main index b44c62c..1eaf0ad 100755 --- a/sats/main +++ b/sats/main @@ -6,25 +6,18 @@ ROOT=$(dirname `realpath $0`); ROOT=`dirname $ROOT`; -# (2) Check config file # -test ! -f $ROOT/com/config/sats && echo "Daemon not started" && exit 1; - -# (3) Fetch BUFFERS # -source $ROOT/com/config/sats; - -# (4) Check required arguments +# (2) Check required arguments test $# -lt 1 && echo -e "ERR: Missing argument\n\n\tSATS_ID\tThe id for the bash-socket\n" && exit 1; -# (2) Start server in background +# (2) Start server in background (keep PID) #--------------------------------------------------------# # (1) Start server # $ROOT/com/launch sats $1 44441 44440 9999& - -# (2) Save PID for later # SERV_PID=$!; + # (3) Start daemons #--------------------------------------------------------# # (1) GPIO daemon # @@ -37,7 +30,32 @@ SYSD_PID=$!; -# (4) Attach processes to itself +# (4) Manage ending proccess #--------------------------------------------------------# -trap "kill -9 $SERV_PID 2>/dev/null; kill -9 $GPIO_PID 2>/dev/null; kill -9 $SYSD_PID 2>/dev/null; exit 1;" INT; -trap "kill -9 $SERV_PID 2>/dev/null; kill -9 $GPIO_PID 2>/dev/null; kill -9 $SYSD_PID 2>/dev/null; exit 1;" KILL; +# (1) Kill subprocesses if Ctrl+C # +on_exit(){ + echo 'sats.main.kill'; + + ## {1} Kill GPIO logger ## + echo 'sats.main.gpio.kill'; + kill -9 $GPIO_PID 2>/dev/null; + + ## {2} Kill SYSD logger ## + echo 'sats.main.sysd.kill'; + kill -9 $SYSD_PID 2>/dev/null; + + ## {3} Kill com server ## + echo 'sats.server.kill'; + kill -9 $SERV_PID 2>/dev/null; + + exit 0; +} + +trap "on_exit" INT KILL; + + + +# (5) Bind server to 'main' (this file) +#--------------------------------------------------------# +wait $SERV_PID; +on_exit; \ No newline at end of file