blob: b85b29bf74ca25e3d13d992f309a7dfd1565a57f [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
Scott Baker93639462015-02-19 13:50:06 -080032 if hasattr(config, "observer_pidfile"):
33 pidfile = config.get("observer_pidfile")
34 else:
35 pidfile = "/var/run/xosobserver.pid"
36 try:
37 file(pidfile,"w").write(str(os.getpid()))
38 except:
39 print "failed to create pidfile %s" % pidfile
40
Tony Mack2c911102014-04-16 19:52:09 -040041def main():
42 # Generate command line parser
43 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
Scott Bakerf80c2be2014-09-16 17:34:21 -070044 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
Tony Mack2c911102014-04-16 19:52:09 -040045 help='Run as daemon.')
Scott Baker6ef76152014-04-30 09:40:23 -070046 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
47 # throwing unrecognized argument exceptions
Scott Bakerd71335d2015-02-02 15:10:13 -080048 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
Scott Baker6ef76152014-04-30 09:40:23 -070049 help='Name of config file.')
Tony Mack2c911102014-04-16 19:52:09 -040050 args = parser.parse_args()
Scott Bakerf80c2be2014-09-16 17:34:21 -070051
Tony Mack2c911102014-04-16 19:52:09 -040052 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040053
Scott Bakerf80c2be2014-09-16 17:34:21 -070054 if django_setup: # 1.7
55 django_setup()
56
Tony Mack9b7a8bd2013-06-24 15:08:01 -040057 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040058 backend.run()
59
60if __name__ == '__main__':
61
62 main()