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 |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 4 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 5 | from observer.backend import Backend |
| 6 | from planetstack.config import Config |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 7 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 8 | config = Config() |
| 9 | |
| 10 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 11 | def 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 Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 20 | logdir=os.path.dirname(config.observer_logfile) |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 21 | # when installed in standalone we might not have httpd installed |
| 22 | if not os.path.isdir(logdir): os.mkdir(logdir) |
Tony Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 23 | 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] | 24 | os.dup2(crashlog, 1) |
| 25 | os.dup2(crashlog, 2) |
| 26 | |
| 27 | def 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 Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 32 | # 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 Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 36 | args = parser.parse_args() |
| 37 | |
| 38 | if args.daemon: daemon() |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 39 | |
| 40 | backend = Backend() |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 41 | backend.run() |
| 42 | |
| 43 | if __name__ == '__main__': |
| 44 | |
| 45 | main() |