blob: 596ecdd368565d6c5d9e51cba7ce2bf5092a080a [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.')
Scott Baker6ef76152014-04-30 09:40:23 -070032 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
33 # throwing unrecognized argument exceptions
34 parser.add_argument('-C', '--config', dest='config_file', action='store', default="/opt/planetstack/plstackapi_config",
35 help='Name of config file.')
Tony Mack2c911102014-04-16 19:52:09 -040036 args = parser.parse_args()
37
38 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040039
40 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040041 backend.run()
42
43if __name__ == '__main__':
44
45 main()