diff --git a/benchmarks/backgrounds/Snakefile b/benchmarks/backgrounds/Snakefile
index d50b8c7fbeeeebeabde184630644bef3faa5d4eb..d44e695c7e7533eba67186451b543cd511895c50 100644
--- a/benchmarks/backgrounds/Snakefile
+++ b/benchmarks/backgrounds/Snakefile
@@ -72,6 +72,13 @@ rule backgrounds_ecal_backwards:
threads: workflow.cores
shell:
"""
+set -m # monitor mode to prevent lingering shells
+cleanup() {{
+ echo Cleaning up
+ kill $WORKER_PID $SCHEDULER_PID
+}}
+trap cleanup EXIT
+
PORT=$RANDOM
dask scheduler --port $PORT &
export DASK_SCHEDULER=localhost:$PORT
@@ -87,5 +94,4 @@ PROTON_BEAM_GAS_GEN=$(realpath {input.proton_beam_gas_gen}) \
PROTON_BEAM_GAS_SIM=$(realpath {input.proton_beam_gas_sim}) \
OUTPUT_DIR={output} \
python {input.script}
-kill $WORKER_PID $SCHEDULER_PID
"""
diff --git a/benchmarks/ecal_gaps/Snakefile b/benchmarks/ecal_gaps/Snakefile
index a312ac1e80ba8f03070a49ae5f79b9c9fa3d4109..b649f15de5b53169ca79c8d051bde9cfc73c02ca 100644
--- a/benchmarks/ecal_gaps/Snakefile
+++ b/benchmarks/ecal_gaps/Snakefile
@@ -66,6 +66,7 @@ rule ecal_gaps:
threads: workflow.cores
shell:
"""
+set -m # monitor mode to prevent lingering shells
cleanup() {{
echo Cleaning up
kill $WORKER_PID $SCHEDULER_PID