2014-02-20 00:05:35 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# this is mostly based off of the code found here:
|
|
|
|
# http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
|
|
|
|
|
|
|
|
import os
|
|
|
|
import resource
|
2018-08-08 06:48:03 +02:00
|
|
|
import signal
|
2014-02-20 00:05:35 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
UMASK = 0
|
|
|
|
WORKDIR = "/"
|
|
|
|
MAXFD = 1024
|
|
|
|
|
|
|
|
if (hasattr(os, "devnull")):
|
|
|
|
REDIRECT_TO = os.devnull
|
|
|
|
else:
|
|
|
|
REDIRECT_TO = "/dev/null"
|
|
|
|
|
|
|
|
def createDaemon():
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
2015-08-27 20:58:51 +02:00
|
|
|
except OSError as e:
|
2017-01-05 00:55:57 +01:00
|
|
|
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
2014-02-20 00:05:35 +01:00
|
|
|
|
|
|
|
if (pid == 0):
|
|
|
|
os.setsid()
|
|
|
|
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
2015-08-27 20:58:51 +02:00
|
|
|
except OSError as e:
|
2017-01-05 00:55:57 +01:00
|
|
|
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
2014-02-20 00:05:35 +01:00
|
|
|
|
|
|
|
if (pid == 0):
|
|
|
|
os.chdir(WORKDIR)
|
|
|
|
os.umask(UMASK)
|
|
|
|
else:
|
|
|
|
f = open('/var/run/ansible_test_service.pid', 'w')
|
|
|
|
f.write("%d\n" % pid)
|
|
|
|
f.close()
|
|
|
|
os._exit(0)
|
|
|
|
else:
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
|
|
|
|
if (maxfd == resource.RLIM_INFINITY):
|
|
|
|
maxfd = MAXFD
|
2017-01-05 00:55:57 +01:00
|
|
|
|
2014-02-20 00:05:35 +01:00
|
|
|
for fd in range(0, maxfd):
|
|
|
|
try:
|
|
|
|
os.close(fd)
|
|
|
|
except OSError: # ERROR, fd wasn't open to begin with (ignored)
|
|
|
|
pass
|
|
|
|
|
|
|
|
os.open(REDIRECT_TO, os.O_RDWR)
|
|
|
|
os.dup2(0, 1)
|
|
|
|
os.dup2(0, 2)
|
|
|
|
|
|
|
|
return(0)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2018-08-08 06:48:03 +02:00
|
|
|
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
|
|
|
2014-02-20 00:05:35 +01:00
|
|
|
retCode = createDaemon()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
time.sleep(1000)
|
|
|
|
|