-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_daemon.py
More file actions
executable file
·52 lines (38 loc) · 1.31 KB
/
run_daemon.py
File metadata and controls
executable file
·52 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# To kick off the script, run the following from the python directory:
# PYTHONPATH=`pwd` python testdaemon.py start
#standard python libs
import logging
import logging.handlers
import time, os, sys
#third party libs
from daemon import runner
#local libs
import digit
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/var/run/digit/digit.pid'
if not os.path.exists('/var/run/digit'):
os.makedirs('/var/run/digit')
self.pidfile_timeout = 5
def run(self):
while True:
logger.info("------------------------------------")
#Main code goes here ...
digit.do_main_program()
app = App()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s|%(name)s|%(levelname)s: %(message)s")
handler = logging.handlers.RotatingFileHandler('digit.log',maxBytes=400000, backupCount=3)
#handler = logging.FileHandler('digit.log')
#handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)
daemon_runner = runner.DaemonRunner(app)
#This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()