From 27a6745743e53ef65a69477d3b12207f07a0d11d Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 31 Oct 2017 21:58:35 -0700 Subject: [PATCH] Passing the Unix SIGTERM signal through to child processes like Borg. --- NEWS | 4 ++++ borgmatic/commands/borgmatic.py | 2 ++ borgmatic/signals.py | 17 +++++++++++++++++ setup.py | 2 +- 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 borgmatic/signals.py diff --git a/NEWS b/NEWS index efb0cd3..ba87af7 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +1.1.10.dev0 + * Passing the Unix SIGTERM signal through to child processes like Borg. This means that Borg now + properly shuts down if borgmatic is terminated (e.g. due to a system suspend). + 1.1.9 * #16, #38: Support for user-defined hooks before/after backup, or on error. * #33: Improve clarity of logging spew at high verbosity levels. diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 3b74b96..1d66c26 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -8,6 +8,7 @@ import sys from borgmatic.borg import check, create, prune from borgmatic.commands import hook from borgmatic.config import collect, convert, validate +from borgmatic.signals import configure_signals from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS, verbosity_to_log_level @@ -120,6 +121,7 @@ def run_configuration(config_filename, args): # pragma: no cover def main(): # pragma: no cover try: + configure_signals() args = parse_arguments(*sys.argv[1:]) logging.basicConfig(level=verbosity_to_log_level(args.verbosity), format='%(message)s') diff --git a/borgmatic/signals.py b/borgmatic/signals.py new file mode 100644 index 0000000..aeb2f93 --- /dev/null +++ b/borgmatic/signals.py @@ -0,0 +1,17 @@ +import os +import signal + + +def _handle_signal(signal_number, frame): + ''' + Send the signal to all processes in borgmatic's process group, which includes child process. + ''' + os.killpg(os.getpgrp(), signal_number) + + +def configure_signals(): + ''' + Configure borgmatic's signal handlers to pass relevant signals through to any child processes + like Borg. + ''' + signal.signal(signal.SIGTERM, _handle_signal) diff --git a/setup.py b/setup.py index a03408d..1cfb11e 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages -VERSION = '1.1.9' +VERSION = '1.1.10.dev0' setup(