]> kaliko git repositories - python-daemon.git/blob - doc/examples/daemon-example.py
Adjust licence in file's header
[python-daemon.git] / doc / examples / daemon-example.py
1 #!/usr/bin/env python
2
3 import sys, time
4 from seth import Daemon
5
6 class MyDaemon(Daemon):
7
8     def __init__(self, pid, log):
9       Daemon.__init__(self, pid, stdout=log, stderr=log)
10
11     def run(self):
12         """Overrides Daemon().run() with actions you want to daemonize.
13         MyDaemon.run() is then called within MyDaemon().start()"""
14         print('Starting Deamon!')  # message issued on self.stdout
15         while True:
16             time.sleep(1)
17             sys.stderr.write('œ unicode write test to stderr\n')
18             sys.stdout.write('write test to stdout\n')
19
20     def shutdown(self):
21         """Overrides Daemon().shutdown() with some clean up"""
22         print("Stopping Daemon!")  # message issued on self.stdout
23
24 if __name__ == '__main__':
25     daemon = MyDaemon('/tmp/daemon-example.pid',
26             '/tmp/daemon.log')
27     if len(sys.argv) == 2:
28         if 'start' == sys.argv[1]:
29             daemon.start()
30         elif 'stop' == sys.argv[1]:
31             daemon.stop()
32         elif 'restart' == sys.argv[1]:
33             daemon.restart()
34         else:
35             print('Unknown command')
36             sys.exit(2)
37         sys.exit(0)
38     else:
39         print("usage: {} start|stop|restart".format(sys.argv[0]))
40         sys.exit(2)