+# Enable job monitor mode.
+# Here we are mostly interested in the following from the monitor mode:
+# All processes run in a separate process group.
+# This allows us to ensure that all subprocesses from all background tasks are
+# cleaned up correctly using signal to process group id.
+set -m
+
+kill_background_jobs ()
+{
+ local pids
+ pids=$(jobs -p)
+
+ if [ -z "$pids" ]; then
+ # Empty
+ return 0
+ fi
+
+ while read -r pid;
+ do
+ # Use negative number to send the signal to the process group.
+ # This ensure that any subprocesses receive the signal.
+ # /dev/null is used since there is an acceptable race between
+ # the moments the pids are listed and the moment we send a
+ # signal.
+ kill -SIGTERM -- "-$pid" 2>/dev/null
+ done <<< "$pids"
+}
+
+function cleanup ()