+apache_restarter

This commit is contained in:
Dmitry Belyaev 2021-06-17 14:49:59 +03:00
parent bba64ac49c
commit 49bf010b87
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 52 additions and 0 deletions

View File

@ -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()

View File

@ -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