Linux守护进程 [英] Linux daemonize

查看:93
本文介绍了Linux守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Linux守护程序.我找到了两种方法.

I am writing a Linux daemon . I found two ways to do it.

  1. 通过调用fork()并设置sid来守护进程.
  2. 使用&运行程序.
  1. Daemonize your process by calling fork() and setting sid.
  2. Running your program with &.

哪种方法是正确的?

推荐答案

来自

以下是成为守护程序的步骤:

Here are the steps to become a daemon:

  1. fork(),以便父级可以退出,这会将控制权返回给命令行或shell来调用您的程序.需要执行此步骤,以确保新流程不会成为流程组负责人.如果您是流程组负责人,则下一步,setsid()将失败.
  2. setsid()成为进程组和会话组负责人.由于控制终端与会话相关联,并且此新会话尚未获得控制终端,因此我们的进程现在没有控制终端,这对于守护程序来说是一件好事.
  3. 再次
  4. fork(),以便父级(会话组负责人)可以退出.这意味着我们作为非会议小组的负责人,永远无法重新获得控制终端.
  5. chdir("/")以确保我们的进程不保留任何目录在使用中.如果不这样做,可能会导致管理员无法卸载文件系统,因为它是我们的当前目录. [等效地,我们可以切换到包含对守护程序的操作很重要的文件的任何目录.]
  6. umask(0),这样我们就可以完全控制所写内容的权限.我们不知道我们可能继承了什么umask. [此步骤是可选的]
  7. close()fds 0、1和2.这释放了我们从父进程继承的标准输入,输出和错误.我们无法知道这些fds可能被重定向到了哪里.请注意,许多守护程序都使用sysconf()确定限制_SC_OPEN_MAX. _SC_OPEN_MAX告诉您最大打开文件数/进程数.然后,在一个循环中,守护程序可以关闭所有可能的文件描述符.您必须决定是否需要执行此操作.如果您认为可能打开了文件描述符,则应该关闭它们,因为并发文件描述符的数量受到限制.
  8. 为stdin,stdout和stderr建立新的打开描述符.即使您不打算使用它们,将它们打开也是一个好主意.精确地处理这些只是品味问题.例如,如果您有一个日志文件,则可能希望以stdout或stderr的形式打开它,并以stdin的形式打开"/dev/null";或者,您可以将'/dev/console'作为stderr和/或stdout打开,并将'/dev/null'作为stdin打开,或者对您的特定守护程序有意义的任何其他组合.
  1. fork() so the parent can exit, this returns control to the command line or shell invoking your program. This step is required so that the new process is guaranteed not to be a process group leader. The next step, setsid(), fails if you're a process group leader.
  2. setsid() to become a process group and session group leader. Since a controlling terminal is associated with a session, and this new session has not yet acquired a controlling terminal our process now has no controlling terminal, which is a Good Thing for daemons.
  3. fork() again so the parent, (the session group leader), can exit. This means that we, as a non-session group leader, can never regain a controlling terminal.
  4. chdir("/") to ensure that our process doesn't keep any directory in use. Failure to do this could make it so that an administrator couldn't unmount a filesystem, because it was our current directory. [Equivalently, we could change to any directory containing files important to the daemon's operation.]
  5. umask(0) so that we have complete control over the permissions of anything we write. We don't know what umask we may have inherited. [This step is optional]
  6. close() fds 0, 1, and 2. This releases the standard in, out, and error we inherited from our parent process. We have no way of knowing where these fds might have been redirected to. Note that many daemons use sysconf() to determine the limit _SC_OPEN_MAX. _SC_OPEN_MAX tells you the maximun open files/process. Then in a loop, the daemon can close all possible file descriptors. You have to decide if you need to do this or not. If you think that there might be file-descriptors open you should close them, since there's a limit on number of concurrent file descriptors.
  7. Establish new open descriptors for stdin, stdout and stderr. Even if you don't plan to use them, it is still a good idea to have them open. The precise handling of these is a matter of taste; if you have a logfile, for example, you might wish to open it as stdout or stderr, and open '/dev/null' as stdin; alternatively, you could open '/dev/console' as stderr and/or stdout, and '/dev/null' as stdin, or any other combination that makes sense for your particular daemon.

更好的是,只需调用 daemon()函数(如果可用).

Better yet, just call the daemon() function if it's available.

这篇关于Linux守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆