blob: 8a2eff23b858bb26a34d7b590b5d305853ab2c3b [file] [log] [blame]
Tony Mack9b7a8bd2013-06-24 15:08:01 -04001#!/usr/bin/env python
2import os
Tony Mack2c911102014-04-16 19:52:09 -04003import argparse
Tony Mack9b7a8bd2013-06-24 15:08:01 -04004os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
Tony Mack2c911102014-04-16 19:52:09 -04005from observer.backend import Backend
6from planetstack.config import Config
Tony Mack9b7a8bd2013-06-24 15:08:01 -04007
Tony Mack2c911102014-04-16 19:52:09 -04008config = Config()
9
10# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
11def daemon():
12 """Daemonize the current process."""
13 if os.fork() != 0: os._exit(0)
14 os.setsid()
15 if os.fork() != 0: os._exit(0)
16 os.umask(0)
17 devnull = os.open(os.devnull, os.O_RDWR)
18 os.dup2(devnull, 0)
19 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
Tony Mack6eb1ef82014-04-16 20:47:20 -040020 logdir=os.path.dirname(config.observer_logfile)
Tony Mack2c911102014-04-16 19:52:09 -040021 # when installed in standalone we might not have httpd installed
22 if not os.path.isdir(logdir): os.mkdir(logdir)
Tony Mack6eb1ef82014-04-16 20:47:20 -040023 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
Tony Mack2c911102014-04-16 19:52:09 -040024 os.dup2(crashlog, 1)
25 os.dup2(crashlog, 2)
26
27def main():
28 # Generate command line parser
29 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
30 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
31 help='Run as daemon.')
32 args = parser.parse_args()
33
34 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040035
36 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040037 backend.run()
38
39if __name__ == '__main__':
40
41 main()