I know os.setsid() is to change the process(forked) group id to itself, but why we need it?
I can see some answer from Google is: To keep the child process running while the parent process exit.
But according to my test below, without os.setsid() the child process won't exit as well even if the parent process exit(or being killed). So why we need to add os.setsid()? Thanks.
import os
import time
import sys
mainPid = os.getpid()
print("Main Pid: %s" % mainPid)
pid = os.fork()
if pid > 0:
time.sleep(3)
print("Main process quit")
sys.exit(0)
#os.setsid()
for x in range(1, 10):
print("spid: %s, ppid: %s pgid: %s" % (os.getpid(), os.getppid(), os.getpgid(0)))
time.sleep(1)
os.setsid()
will not make much difference. For example you can usesetsid
when youssh
to a host to launch a command you don't want terminating when you exit your ssh session (an alternative would benohup
)Main_Python_Process >> Your_Process
Can you kill your_process without killing main_python_process ? How to give your process control toOS
?