diff --git a/apache_restarter/apache_restarter.py b/apache_restarter/apache_restarter.py new file mode 100644 index 0000000..a6828e4 --- /dev/null +++ b/apache_restarter/apache_restarter.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import logging +import os +import sys +import time + +delay = 2 + + +def restart_apache(): + os.system("systemctl reload-or-restart apache2") + + +def run_monitor(filename): + while True: + if os.path.exists(filename): + logging.info("restarting apache") + restart_apache() + time.sleep(delay) + os.remove(filename) + time.sleep(delay) + + +def main(): + logging.basicConfig(level=logging.INFO, format='%(asctime)s: %(message)s') + if len(sys.argv) < 2: + raise Exception('No monitor filename') + monitor_filename = sys.argv[1] + monitor_dir = os.path.dirname(monitor_filename) + if not os.path.isdir(monitor_dir): + raise Exception(f'directory not exist: {monitor_dir}') + if not os.access(monitor_dir, os.W_OK): + raise Exception(f'directory not writable: {monitor_dir}') + run_monitor(monitor_filename) + + +if __name__ == "__main__": + main() diff --git a/apache_restarter/apache_restarter.service b/apache_restarter/apache_restarter.service new file mode 100644 index 0000000..c506368 --- /dev/null +++ b/apache_restarter/apache_restarter.service @@ -0,0 +1,13 @@ +[Unit] +Description=Apache restart monitor +After=apache2.service +Wants=apache2.service + +[Service] +ExecStart=/usr/bin/env python3 /path/apache_restarter.py /run/apache_restart/apache_restart +ExecReload=/bin/kill -HUP $MAINPID +Restart=always +RestartSec=2s + +[Install] +WantedBy=multi-user.target