pub1c-web/apache_restarter/apache_restarter.py

40 lines
944 B
Python
Raw Permalink Normal View History

2021-06-17 11:49:59 +00:00
#!/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()