c5f37f6bd6
* service tests: check that daemon is really running (spoiler: it isn't) * service tests: add PIDFile directive in systemd unit * service tests: check 'changed' too * service tests: fix indentation & use changed test * service tests: #16694 has been fixed a long time ago * service tests: refactor - always execute cleaning tasks - move tests tasks in a dedicated file * service tests: add test for #42786 * service tests: display value of ansible_service_mgr * service tests: allow to run tests twice in a row stop & disable ansible test service * service tests: 'pattern' value must be a substring 'pattern' parameter is poorly named * service tests: check ansible_test service status * service tests: test daemon must handle SIGHUP because 'initctl reload' sends SIGHUP, otherwise test daemon stops when receiving the signal * service tests: remove upstart override file too and check that files were removed using raw module and stat command
70 lines
1.4 KiB
Python
Executable file
70 lines
1.4 KiB
Python
Executable file
#!/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
|
|
import signal
|
|
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()
|
|
except OSError as e:
|
|
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
|
|
|
if (pid == 0):
|
|
os.setsid()
|
|
|
|
try:
|
|
pid = os.fork()
|
|
except OSError as e:
|
|
raise Exception("%s [%d]" % (e.strerror, e.errno))
|
|
|
|
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
|
|
|
|
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__":
|
|
|
|
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
|
|
retCode = createDaemon()
|
|
|
|
while True:
|
|
time.sleep(1000)
|
|
|