Commit aeecbf173147d0eef22961c807f55c756417235c
1 parent
85140b50
mraptor_milter: added daemonize to run as a Unix daemon
Showing
1 changed file
with
37 additions
and
2 deletions
oletools/mraptor_milter.py
| ... | ... | @@ -50,8 +50,18 @@ http://www.decalage.info/python/oletools |
| 50 | 50 | # 2016-08-08 v0.01 PL: - first version |
| 51 | 51 | # 2016-08-12 v0.02 PL: - added logging to file with time rotation |
| 52 | 52 | # - archive each e-mail to a file before filtering |
| 53 | +# 2016-08-30 v0.03 PL: - added daemonize to run as a Unix daemon | |
| 53 | 54 | |
| 54 | -__version__ = '0.02' | |
| 55 | +__version__ = '0.03' | |
| 56 | + | |
| 57 | +# --- TODO ------------------------------------------------------------------- | |
| 58 | + | |
| 59 | +# TODO: option to run in the foreground for troubleshooting | |
| 60 | +# TODO: option to write logs to the console | |
| 61 | +# TODO: options to set listening port and interface | |
| 62 | +# TODO: config file for all parameters | |
| 63 | +# TODO: option to run as a non-privileged user | |
| 64 | +# TODO: handle files in archives | |
| 55 | 65 | |
| 56 | 66 | |
| 57 | 67 | # --- IMPORTS ---------------------------------------------------------------- |
| ... | ... | @@ -92,6 +102,11 @@ LOGFILE_PATH = os.path.join(LOGFILE_DIR, LOGFILE_NAME) |
| 92 | 102 | ARCHIVE_DIR = '/var/log/mraptor_milter' |
| 93 | 103 | # ARCHIVE_DIR = '.' |
| 94 | 104 | |
| 105 | +# file to store PID for daemonize | |
| 106 | +PIDFILE = "/tmp/mraptor_milter.pid" | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 95 | 110 | # === LOGGING ================================================================ |
| 96 | 111 | |
| 97 | 112 | # Set up a specific logger with our desired output level |
| ... | ... | @@ -319,6 +334,12 @@ def main(): |
| 319 | 334 | print('mraptor_milter v%s - http://decalage.info/python/oletools' % __version__) |
| 320 | 335 | print('logging to file %s' % LOGFILE_PATH) |
| 321 | 336 | print('Press Ctrl+C to stop.') |
| 337 | + | |
| 338 | + # make sure the log directory exists: | |
| 339 | + try: | |
| 340 | + os.makedirs(LOGFILE_DIR) | |
| 341 | + except: | |
| 342 | + pass | |
| 322 | 343 | # Add the log message handler to the logger |
| 323 | 344 | # log to files rotating once a day: |
| 324 | 345 | handler = logging.handlers.TimedRotatingFileHandler(LOGFILE_PATH, when='D', encoding='utf8') |
| ... | ... | @@ -343,5 +364,19 @@ def main(): |
| 343 | 364 | Milter.runmilter("mraptor_milter", SOCKET, TIMEOUT) |
| 344 | 365 | log.info('Stopping mraptor_milter.') |
| 345 | 366 | |
| 367 | + | |
| 346 | 368 | if __name__ == "__main__": |
| 347 | - main() | |
| 369 | + | |
| 370 | + # Using daemonize: | |
| 371 | + # See http://daemonize.readthedocs.io/en/latest/ | |
| 372 | + from daemonize import Daemonize | |
| 373 | + daemon = Daemonize(app="mraptor_milter", pid=PIDFILE, action=main) | |
| 374 | + daemon.start() | |
| 375 | + | |
| 376 | + # Using python-daemon - Does not work as-is, need to create the PID file | |
| 377 | + # See https://pypi.python.org/pypi/python-daemon/ | |
| 378 | + # See PEP-3143: https://www.python.org/dev/peps/pep-3143/ | |
| 379 | + # import daemon | |
| 380 | + # import lockfile | |
| 381 | + # with daemon.DaemonContext(pidfile=lockfile.FileLock(PIDFILE)): | |
| 382 | + # main() | ... | ... |