From 0061751efe497a60faf45524872624480b2a54a6 Mon Sep 17 00:00:00 2001
From: Carmelo Pellegrino <carmelo.pellegrino@gmail.com>
Date: Wed, 20 May 2015 11:37:05 +0200
Subject: [PATCH] clean-up implemented through trap on exit

---
 patch-control.sh | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/patch-control.sh b/patch-control.sh
index 3d8f4e4..98dbb47 100644
--- a/patch-control.sh
+++ b/patch-control.sh
@@ -24,6 +24,16 @@ function test_lock() {
   fi
 }
 
+function on_exit() {
+  if [ "${LOCK_OWNER}" == "True" ]; then
+    rm_lock
+  fi
+
+  for pid in ${PROCS}; do
+    kill -9 ${pid}
+  done
+}
+
 function createList() {
   local LIST
   local file
@@ -73,6 +83,9 @@ function get_run_number() {
 test_lock || { zenity --error --text="There is another instance running of this program."; exit 1;}
 
 put_lock
+LOCK_OWNER="True"
+
+trap on_exit EXIT
 
 while [ 1 ]; do
 
@@ -88,7 +101,6 @@ while [ 1 ]; do
 
   if [ -z "${rs_file}" ]; then
     zenity --error  --text="No Run Setup file selectet, aborting"
-    rm_lock
     exit -1
   fi
 
@@ -125,6 +137,7 @@ run_number=`get_run_number`
 
 daq_log_file=~/.km3_private/daq_${run_number}.log
 JGetMessage -H localhost -T MSG -d 3 >${daq_log_file} &
+PROCS=(${PROCS} $!)
 ## impl: get the pid and add to the list of must_kill_at_the_exit
 
 # Copy selected run setup file, changing the run number
@@ -134,6 +147,7 @@ run_start_time=`nowUTC`
 
 # Launch the DM
 dm_driver ${run_duration} | mono DetectorControl.exe --control --reset &
+PROCS=(${PROCS} $!)
 
 # Launch the DAQ
 log "Launching the DAQ with the following command line: daq.sh ${run_number} ${run_start_time} ${run_duration}"
@@ -149,6 +163,4 @@ sleep ${run_duration}
 
 zenity --info --text="Run ${run_number} finished!"
 
-rm_lock
-
 exit 0
-- 
GitLab