+##############
+#
+# Cleanup on signal
+#
+#
+
+# Global variable. Stores which process group should be killed (or None otherwise)
+pgtokill = None
+
+def kill_process_group(pgid):
+ # print("Kill process group {}".format(pgid))
+ try:
+ os.killpg(pgid, signal.SIGTERM)
+ except OSError:
+ # os.killpg failed. OK. Some subprocesses may still be running.
+ pass
+
+def signal_handler(signal, frame):
+ print("Caught signal {}".format(SIGNALS_TO_NAMES_DICT[signal]))
+ if pgtokill is not None:
+ kill_process_group(pgtokill)
+ tesh_exit(5)
+
+