Linux 如何制作进程守护进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5384168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:33:21  来源:igfitidea点击:

how to make a process daemon

clinuxdaemonsystems-programming

提问by Registered User

I am trying to understand how can I make my program a daemon.So some things which I came across are In general, a program performs the following steps to become a daemon:

我试图了解如何使我的程序成为守护程序。所以我遇到的一些事情是一般来说,程序执行以下步骤成为守护程序:

  1. Call fork( ).
  2. In the parent, call exit( ). This ensures that the original parent (the daemon's grandparent) is satisfied that its child terminated, that the daemon's parent is no longer running, and that the daemon is not a process group leader. This last point is a requirement for the successful completion of the next step.

  3. Call setsid( ), giving the daemon a new process group and session, both of which have it as leader. This also ensures that the process has no associated controlling terminal (as the process just created a new session, and will not assign one).

  4. Change the working directory to the root directory via chdir( ). This is done because the inherited working directory can be anywhere on the filesystem. Daemons tend to run for the duration of the system's uptime, and you don't want to keep some random directory open, and thus prevent an administrator from unmounting the filesystem containing that directory.

  5. Close all file descriptors.

  6. Open file descriptors 0, 1, and 2 (standard in, standard out, and standard error) and redirect them to /dev/null.
  1. 打电话fork( )
  2. 在父级中,调用exit( ). 这确保了原始父进程(守护进程的祖父进程)对它的子进程终止、守护进程的父进程不再运行以及守护进程不是进程组领导感到满意。最后一点是成功完成下一步的必要条件。

  3. 调用setsid( ),为守护进程提供一个新的进程组和会话,两者都将其作为领导者。这也确保进程没有关联的控制终端(因为进程刚刚创建了一个新会话,不会分配一个)。

  4. 通过 将工作目录更改为根目录chdir( )。这样做是因为继承的工作目录可以位于文件系统上的任何位置。守护进程往往会在系统正常运行期间运行,您不想让某个随机目录保持打开状态,从而防止管理员卸载包含该目录的文件系统。

  5. 关闭所有文件描述符。

  6. 打开文件描述符 0、1 和 2(标准输入、标准输出和标准错误)并将它们重定向到/dev/null.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>

int main (void)
{
    pid_t pid;
    int i;

    /* create new process */
    pid = fork ( );  
    if (pid == -1)  
        return -1;  
    else if (pid != 0)  
        exit (EXIT_SUCCESS);  

    /* create new session and process group */  
    if (setsid ( ) == -1)  
        return -1;  

    /* set the working directory to the root directory */  
    if (chdir ("/") == -1)  
        return -1;  

    /* close all open files--NR_OPEN is overkill, but works */  
    for (i = 0; i < NR_OPEN; i++)  
        close (i);  

    /* redirect fd's 0,1,2 to /dev/null */  
    open ("/dev/null", O_RDWR);  
    /* stdin */  
    dup (0);  
    /* stdout */  
    dup (0);  
    /* stderror */  

    /* do its daemon thing... */  

    return 0;  
}

Can some one give me a link to existing source code of some program like Apache so that I can understand this process in more depth.

有人可以给我一个指向某些程序(如 Apache)的现有源代码的链接,以便我可以更深入地了解这个过程。

回答by deadbeef

If you are looking for a clean approach please consider using standard api- int daemon(int nochdir, int noclose);. Man page pretty simple and self explanatory. man page. A well tested api far outweigh our own implementation interms of portability and stability.

如果您正在寻找一种干净的方法,请考虑使用标准 api- int daemon(int nochdir, int noclose);。手册页非常简单且不言自明。手册页。一个经过良好测试的 api 在可移植性和稳定性方面远远超过我们自己的实现。

回答by Alok Prasad

In Linux, it can be easily done using:

在 Linux 中,可以使用以下方法轻松完成:

int main(int argc, char* argv[])
{
    daemon(0,0);
    while(1)
    {
        sleep(10)
        /*do something*/
    }

    return 0;
}