I wrote a simple C program to create orphan process:
int main(){
int pid = fork();
if(pid == 0){
execl("/usr/bin/firefox", "firefox", (char*)0);
}else{
sleep(2);
return 0;
}
}
I compile this file to a.out
and run the following command in terminal:
gnome-terminal -- ./a.out
This opens a new terminal and firefox
, but after 2s the terminal exits and firefox
terminates, but I want firefox
to be an orphan process with terminal exiting.
My program is correct, because when I tried
./a.out
directly in terminal, firefox
opens and when I close the present terminal manually, firefox
is still there. So it must be a problem of gnome-terminal -- ...
.
I also replaced gnome-terminal --
with xterm -e
, but things are the same.
Is there any way to open a new terminal with a.out
run in the new terminal window and make firefox
an orphan?(I know how to execute a.out
in a new terminal and preserve the new terminal after a.out
return, but I want exit the new terminal and keep firefox
an orphan)
.
Firefox is getting killed by SIGHUP
because its controlling terminal goes away when gnome-terminal
or xterm
exits. You have two options to stop this:
nohup
does: Set Firefox to ignore SIGHUP
by doing signal(SIGHUP, SIG_IGN);
before your execl
.setsid()
before your execl
so that the process doesn't have a controlling terminal. Note that this might result in Firefox suddenly acquiring a controlling terminal later if it happens to open
one for some reason.