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 | |
| 32 | def main(): |
| 33 | # Generate command line parser |
| 34 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 35 | parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False, |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 36 | help='Run as daemon.') |
Scott Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 37 | # 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 Baker | d71335d | 2015-02-02 15:10:13 -0800 | [diff] [blame] | 39 | 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] | 40 | help='Name of config file.') |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 41 | args = parser.parse_args() |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 42 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 43 | if args.daemon: daemon() |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 44 | |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 45 | if django_setup: # 1.7 |
| 46 | django_setup() |
| 47 | |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 48 | backend = Backend() |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 49 | backend.run() |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | |
| 53 | main() |