Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import os |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 3 | import argparse |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 5 | from observer.backend import Backend |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 6 | from xos.config import Config, DEFAULT_CONFIG_FN |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 7 | |
| 8 | try: |
| 9 | from django import setup as django_setup # django 1.7 |
| 10 | except: |
| 11 | django_setup = False |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 12 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 13 | config = Config() |
| 14 | |
| 15 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 16 | def 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 Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 25 | logdir=os.path.dirname(config.observer_logfile) |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 26 | # when installed in standalone we might not have httpd installed |
| 27 | if not os.path.isdir(logdir): os.mkdir(logdir) |
Tony Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 28 | crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 29 | os.dup2(crashlog, 1) |
| 30 | os.dup2(crashlog, 2) |
| 31 | |
Scott Baker | 9363946 | 2015-02-19 13:50:06 -0800 | [diff] [blame] | 32 | 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 Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 41 | def main(): |
| 42 | # Generate command line parser |
| 43 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 44 | parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False, |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 45 | help='Run as daemon.') |
Scott Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 46 | # 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 Baker | d71335d | 2015-02-02 15:10:13 -0800 | [diff] [blame] | 48 | parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN, |
Scott Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 49 | help='Name of config file.') |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 50 | args = parser.parse_args() |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 51 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 52 | if args.daemon: daemon() |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 53 | |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 54 | if django_setup: # 1.7 |
| 55 | django_setup() |
| 56 | |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 57 | backend = Backend() |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 58 | backend.run() |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | |
| 62 | main() |