-
Notifications
You must be signed in to change notification settings - Fork 4
/
systemd-test
33 lines (29 loc) · 920 Bytes
/
systemd-test
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
#!/usr/bin/env python3
import os
import socket
cwd = os.path.dirname(__file__)
LAUNCH = 'httpd-true'
if __name__ == "__main__":
# Bind a TCP/ip socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Tell the OS to allow instant reuse of the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('localhost', 8080))
# We don't listen to the socket yet. We just prepare it for another
# application
fd = s.fileno()
# Clone the FD as 3
# 0 == stdin
# 1 == stdout
# 2 == stderr
# Extra fd start from there.
os.dup2(fd, 3)
pid = os.fork()
if pid == 0: # In child
pid = os.getpid()
print("Child pid = %s" % pid)
os.environ['LISTEN_PID'] = str(pid)
os.execv(os.path.join(cwd, LAUNCH), [LAUNCH])
# In parent, we just wait
ret = os.waitpid(pid, 0)
print("Execed child returned %s" % (ret,))