Answer the question
In order to leave comments, you need to log in
Why is pid being killed?
Hello everyone, I am writing a simple daemon for one function that will parse
In general, I write pid and immediately disappears, in the function that I call an infinite loop, in general, the code will say more than me
import os
import sys
from test import test
pidfile = '/home/maxim/Projects/code/pidfile.pid'
sf = '/dev/null'
class Daemon:
def __init__(self, stdin=sf, stdout=sf, stderr=sf):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
def daemonize(stdin, stdout, stderr):
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError as e:
print(str(e))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError as e:
print(str(e))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = open(stdin, 'r')
so = open(stdout, 'a+')
se = open(stderr, 'a+')
os.dup2(si.fileno(), 0) # sys.stdin
os.dup2(so.fileno(), 1) # sys.stdout
os.dup2(se.fileno(), 2) # sys.stderr
# write pidfile
with open(pidfile, 'w+') as f:
f.write(str(os.getpid()))
def start():
Daemon.daemonize(stdin=sf, stdout=sf, stderr=sf)
Daemon.test()
def test():
while True:
with open('/home/maxim/Projects/code/file.txt', 'w+') as f:
f.write('test')
sleep(2)
Daemon.start()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question