<分区>
Possible Duplicate:
In Linux, how to prevent a background process from being stopped after closing SSH client
我有一个 C 程序,我可以通过终端(通常是从 Linux 机器上的 SSH)访问它并与之交互。我一直在尝试找到问题的解决方案,在我关闭终端/注销后,该过程以它结束(该程序基本上要求一些选项然后继续其业务而无需进一步交互所以我想拥有它即使在我注销 SSH 后继续运行)。
在 Linux 中有一些方法可以避免这种情况,例如“screen”,但我想使用 C 以编程方式完成它,而不依赖于已安装的软件包,例如 screen - 即使这意味着重新发明轮子。
到目前为止,我将 fork()
理解为守护进程的标准简单方法,所以任何人都可以帮助我完成允许上述过程发生的代码吗?
在父级内:
main()
{
//Do interactive stuff
signal(SIGCHLD, SIG_IGN); //stops the parent waiting for the child process to end
if(fork())
exit(0);
// and now the program continues in the child process
我现在可以注销 SSH,这会关闭原来的 shell...而 child 继续它的工作!
在 child 中:
//Continue with processing data/whatever the program does (no input/output to terminal required)
exit(0);