简体   繁体   English

如何在 Linux 上守护 ac/c++ 程序

[英]how a daemonize a c/c++ program on Linux

I wrote a daemon program on Linux according to the guide at http://linux.die.net/man/1/daemonize , but the process crashed several times and I cannot find the reason.我根据http://linux.die.net/man/1/daemonize上的指南在 Linux 上编写了一个守护程序,但是该进程崩溃了几次,我找不到原因。 It has troubled me for a few days.它困扰了我几天。

Today I happened to have read 'UNIX network Programming volume 1, Third Edition' by W.Richard Stevens.今天我碰巧读了 W.Richard Stevens 所著的“UNIX 网络编程第 1 卷,第三版”。 And in this book, it shows an example to write daemon program.在本书中,它展示了一个编写守护程序的例子。 After reading the example, I realized 'Disassociate from the control terminal' is missing from my code.阅读示例后,我意识到我的代码中缺少“与控制终端分离”。

Now my question is to daemonize a process, why we need disassociate from the control terminal?现在我的问题是守护进程,为什么我们需要与控制终端解除关联? And does it related to the crash of the process?它与进程的崩溃有关吗? Is there any other place is missing in my code for daemonize?我的守护进程代码中是否缺少其他任何地方?

Appreciate your replies.感谢您的回复。

Here is my code:这是我的代码:

bool daemonize()
{
    // http://linux.die.net/man/1/daemonize

    // change working dir to root
    (void) uchdir("/");

    // close stdin, stderr, stdout
    if (int fdnull = open("/dev/null", O_RDWR))
    {   
        dup2 (fdnull, STDIN_FILENO);
        dup2 (fdnull, STDOUT_FILENO);
        dup2 (fdnull, STDERR_FILENO);
        close(fdnull);
    }   
    else
    {   
        Log (ERR, "Failed to open /dev/null");
        return false;
    }   

    // detach from previous process group
    if (setsid () == -1)      /* request a new session (job control) */
    {   
        Log (ERR, "Failed to detach from previous process group");
        return false;
    }   

    // inhibit others completely and group write
    umask(027);

    // it's dameonized!
    return true;
}

The basic steps to deamonize a C or C++ program have already been mentioned in this question: Creating a daemon in Linux在这个问题中已经提到了对 C 或 C++ 程序进行 deamonize 的基本步骤: Creating a daemon in Linux

Yes, the question there was for C and not for C++, but since the system calls you need to daemonize a program are C functions in both cases, that really does not make a difference.是的,问题是针对 C 而不是针对 C++,但是由于在这两种情况下您需要守护程序的系统调用都是 C 函数,所以这真的没有区别。

I found this github repository useful, it has what you need to build a daemon:我发现这个 github 存储库很有用,它具有构建守护程序所需的功能:

Simple example of daemon for Linux Linux 守护进程的简单示例

And here is a stack overflow thread why double fork is nessesary.这是一个堆栈溢出线程,为什么叉是必要的。

Not addressing your actual question but…没有解决您的实际问题,但是......

I wrote a daemon program on Linux according to the guide at [ http://linux.die.net/man/1/daemonize][1] ,我根据 [ http://linux.die.net/man/1/daemonize][1]上的指南在 Linux 上编写了一个守护程序,

please don't!请不要!

Programs that daemon -ize are the bane of any sensible init system and service tracker. daemon -ize 的daemon是任何明智的初始化系统和服务跟踪器的祸根。 The problem is, that after a program detaches from its controlling terminal and parent process it gets difficult to keep track about its state and regain control over it.问题是,在程序与其控制终端和父进程分离后,很难跟踪其状态并重新获得对其的控制。 The usual way of using PID files is prone to race conditions, which is especially bad if the intention is to automatically respawn such a process.使用 PID 文件的通常方法容易出现竞争条件,如果意图是自动重新生成这样的进程,这尤其糟糕。

The use of daemon led to the development of several hacks, some outright ugly some kind of okay, but none of them being beautiful. daemon的使用导致了几个黑客的发展,有些完全丑陋,有些还可以,但没有一个是美丽的。 Do yourself and the rest of the world a favour and don't daemonize your program.帮自己和世界上的其他人一个忙,不要守护你的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM