def daemonize(self, args): log.setup(self.log_file) log.info("Daemonizing.") self.before_daemonize(args) if unix.still_running(self.name, pid_file_path=self.pid_path): log.error("%s still running. Aborting." % self.name) sys.exit(1) else: unix.daemonize(self.name, pid_file_path=self.pid_path) def shutdown_handler(signal, frame): self.shutdown(signal) sys.exit(0) unix.register_shutdown(shutdown_handler) self.before_jail(args) log.info("Setting up the chroot jail to: %s" % self.run_dir) unix.chroot_jail(self.run_dir) self.before_drop_privs(args) unix.drop_privileges(uid_name=self.uid, gid_name=self.gid) log.info("Server %s running." % self.name) self.start(args)
def test_daemonize(os__exit, *calls): unix.daemonize("test_daemonize", pid_file_path="/tmp") for i in calls: assert_true(i.called, "Failed to call %r" % i) # should not be calling exit assert_false(os__exit.called)
def test_daemonize_dont_exit(): def callme(): log.setup('/tmp/test_daemonize_no_exit.log') for i in range(0, 3): log.info("I ran!") time.sleep(1) unix.daemonize('test_daemonize_no_exit', pid_file_path="/tmp", dont_exit=True, to_call=callme) assert_true(os.path.exists('/tmp/test_daemonize_no_exit.pid'))
def test_daemonize_dont_exit(): if os.path.exists('/tmp/test_daemonize_no_exit.log'): os.unlink('/tmp/test_daemonize_no_exit.log') if os.path.exists('/tmp/test_daemonize_no_exit.pid'): os.unlink('/tmp/test_daemonize_no_exit.pid') def main(): """This will exit the daemon after 4 seconds.""" log.setup('/tmp/test_daemonize_no_exit.log', force=True) for i in range(0, 4): log.info("I ran!") time.sleep(1) unix.daemonize('test_daemonize_no_exit', pid_file_path="/tmp", dont_exit=True, main=main) while not unix.still_running('test_daemonize_no_exit', pid_file_path='/tmp'): time.sleep(1) assert_true(os.path.exists('/tmp/test_daemonize_no_exit.pid'))