更新时间:2023-02-22 21:37:56
您可能希望标准的Unix守护技术,涉及双叉:
You probably want the standard Unix daemon technique, involving a double fork:
void daemonize(void)
{
if (fork()) exit(0); // fork. parent exits.
setsid(); // become process group leader
if (fork()) _exit(0); // second parent exits.
chdir("/"); // just so we don't mysteriously prevent fs unmounts later
close(0); // close stdin, stdout, stderr.
close(1);
close(2);
}
看起来像现代的Linux机器有守护进程()
库函数presumably做同样的事情。
Looks like modern Linux machines have a daemon()
library function that presumably does the same thing.
这有可能是第一个退出
应 _exit
,但这code一直为我工作
It's possible that the first exit
should be _exit
, but this code has always worked for me.