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

Updated create_daemon() function for cross-platform compatibility

parent f4bddbcc
......@@ -92,15 +92,61 @@ def stream():
return render_template('stream.html', stream_url=stream_url)
def create_daemon():
"""Create a Unix-style daemon process"""
try:
# First fork
pid = os.fork()
if pid > 0:
# Exit first parent
if os.name == 'posix': # Unix-like systems
try:
# First fork
pid = os.fork()
if pid > 0:
# Exit first parent
sys.exit(0)
except OSError as err:
sys.stderr.write(f'Fork #1 failed: {err}\n')
sys.exit(1)
# Decouple from parent environment
os.chdir('/')
os.setsid()
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 OSError as err:
sys.stderr.write(f'Fork #1 failed: {err}\n')
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
......
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