def daemon(action): """ Trigger the daemon, run the action command and return the daemon object if required. 'action' is a string, either start, stop or status Returns an instance of deiman.Deiman """ d = Deiman(options.pid) if action == "stop": d.stop() sys.exit(0) elif action == "status": d.status() sys.exit(0) if len(sys.argv) == 1: print_help() sys.exit(2) if action == "start": d.start() return d else: sys.exit(0)
# Need sys for exit status and argv import sys # Import Deiman from deiman import Deiman # Create a new instance of Deiman, passing in a PID file to use # this only instantiates the class, nothing more. d = Deiman("/tmp/deiman.pid") # Check incoming argv values to see if we have start, stop or status # command. if len(sys.argv) == 1 or sys.argv[1] not in ('start', 'stop', 'status'): print("Usage: python simple-daemon.py [start, stop, status]") sys.exit(2) # Set the incoming action to a shorter variable name, lazy. act = sys.argv[1] # Action is start, so tell Deiman to fork itself off in to the # background. if act == "start": d.start() # Action is stop, this will take the existing PID file, get the # current process ID and stop the running process. elif act == "stop": d.stop() sys.exit(0) # Action is status, this will get the current status information # of the process from /proc. It'll tell you if it's running or not # and also if it's a zombie process.