diff --git a/patch-control.sh b/patch-control.sh
index 3d8f4e4466f0750c92631f7ba441874fa8bbaf4c..98dbb473999c40d68d8ceddcf3bdcd72458e5776 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