blob: 83a392ade5fe8e3df37ae156cd56e7794a7fb99f [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
Scott Baker86e132c2015-02-11 21:38:09 -08004os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Tony Mack2c911102014-04-16 19:52:09 -04005from observer.backend import Backend
Scott Baker86e132c2015-02-11 21:38:09 -08006from xos.config import Config, DEFAULT_CONFIG_FN
Scott Bakerf80c2be2014-09-16 17:34:21 -07007
8try:
9 from django import setup as django_setup # django 1.7
10except:
11 django_setup = False
Tony Mack9b7a8bd2013-06-24 15:08:01 -040012
Tony Mack2c911102014-04-16 19:52:09 -040013config = Config()
14
15# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
16def daemon():
17 """Daemonize the current process."""
18 if os.fork() != 0: os._exit(0)
19 os.setsid()
20 if os.fork() != 0: os._exit(0)
21 os.umask(0)
22 devnull = os.open(os.devnull, os.O_RDWR)
23 os.dup2(devnull, 0)
24 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
Tony Mack6eb1ef82014-04-16 20:47:20 -040025 logdir=os.path.dirname(config.observer_logfile)
Tony Mack2c911102014-04-16 19:52:09 -040026 # when installed in standalone we might not have httpd installed
27 if not os.path.isdir(logdir): os.mkdir(logdir)
Tony Mack6eb1ef82014-04-16 20:47:20 -040028 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
Tony Mack2c911102014-04-16 19:52:09 -040029 os.dup2(crashlog, 1)
30 os.dup2(crashlog, 2)
31
32def main():
33 # Generate command line parser
34 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
Scott Bakerf80c2be2014-09-16 17:34:21 -070035 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
Tony Mack2c911102014-04-16 19:52:09 -040036 help='Run as daemon.')
Scott Baker6ef76152014-04-30 09:40:23 -070037 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
38 # throwing unrecognized argument exceptions
Scott Bakerd71335d2015-02-02 15:10:13 -080039 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
Scott Baker6ef76152014-04-30 09:40:23 -070040 help='Name of config file.')
Tony Mack2c911102014-04-16 19:52:09 -040041 args = parser.parse_args()
Scott Bakerf80c2be2014-09-16 17:34:21 -070042
Tony Mack2c911102014-04-16 19:52:09 -040043 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040044
Scott Bakerf80c2be2014-09-16 17:34:21 -070045 if django_setup: # 1.7
46 django_setup()
47
Tony Mack9b7a8bd2013-06-24 15:08:01 -040048 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040049 backend.run()
50
51if __name__ == '__main__':
52
53 main()