Commit bde2b83d authored by robocoders.ai's avatar robocoders.ai

Updated create_daemon() function for cross-platform compatibility

parent f4bddbcc
...@@ -92,7 +92,7 @@ def stream(): ...@@ -92,7 +92,7 @@ def stream():
return render_template('stream.html', stream_url=stream_url) return render_template('stream.html', stream_url=stream_url)
def create_daemon(): def create_daemon():
"""Create a Unix-style daemon process""" if os.name == 'posix': # Unix-like systems
try: try:
# First fork # First fork
pid = os.fork() pid = os.fork()
...@@ -108,6 +108,52 @@ def create_daemon(): ...@@ -108,6 +108,52 @@ def create_daemon():
os.setsid() os.setsid()
os.umask(0) os.umask(0)
# Second fork
try:
pid = os.fork()
if pid > 0:
# Exit from second parent
sys.exit(0)
except OSError as err:
sys.stderr.write(f'Fork #2 failed: {err}\n')
sys.exit(1)
# Redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = open(os.devnull, 'r')
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
elif os.name == 'nt': # Windows
try:
# Hide the console window
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Start the script as a new process
subprocess.Popen([sys.executable, __file__],
startupinfo=si,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
# Exit the current process
sys.exit(0)
except Exception as err:
sys.stderr.write(f'Failed to create background process: {err}\n')
sys.exit(1)
else:
sys.stderr.write(f'Unsupported operating system: {os.name}\n')
sys.exit(1)
# Decouple from parent environment
os.chdir('/')
os.setsid()
os.umask(0)
# Second fork # Second fork
try: try:
pid = os.fork() pid = os.fork()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment