守护进程时是否需要更改父进程? [英] Is changing parent process necessary when daemonize a process?

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

问题描述

我在 https://en.wikipedia上了解有关守护进程的信息。 .org / wiki / Daemon_%28computing%29#Creation


从严格的技术意义上讲,类似于Unix的系统进程当守护进程
的父进程终止并且该守护进程被分配了初始化
进程(进程号1)作为其父进程并且没有
控制终端时,它是守护进程
。但是,更常见的是,守护进程可能是任何
后台进程,无论是否是init进程的子进程。

In a strictly technical sense, a Unix-like system process is a daemon when its parent process terminates and the daemon is assigned the init process (process number 1) as its parent process and has no controlling terminal. However, more commonly, a daemon may be any background process, whether a child of the init process or not.

在类似Unix的系统上,从命令行或从
启动脚本(例如init脚本或SystemStarter脚本)启动该进程时,使该进程成为
守护程序的常用方法,
涉及:

On a Unix-like system, the common method for a process to become a daemon, when the process is started from the command line or from a startup script such as an init script or a SystemStarter script, involves:


  1. 与控制tty脱离联系

  2. 成为会议负责人

  3. 成为流程组负责人

  4. 通过分叉和退出(一次或两次)作为后台任务执行。有时,这是使该过程成为会话
    领导者所必需的。它还允许父进程继续其正常的
    执行。

  5. 将根目录(/)设置为当前工作目录,以便该进程不保留任何使用中的目录可能在
    a挂载的文件系统上(允许将其卸载)。

  6. 将umask更改为0以允许open(),creat()和其他操作系统调用提供自己的权限掩码,而不是
    依赖于调用者的umask

  7. 在执行时关闭所有由父进程保持打开状态的继承文件,包括标准流(stdin,stdout和stderr)的文件描述符0、1和2
    。所需的文件
    稍后将打开。

  8. 使用日志文件,控制台或/ dev / null作为stdin,stdout和stderr

  1. Dissociating from the controlling tty
  2. Becoming a session leader
  3. Becoming a process group leader
  4. Executing as a background task by forking and exiting (once or twice). This is required sometimes for the process to become a session leader. It also allows the parent process to continue its normal execution.
  5. Setting the root directory (/) as the current working directory so that the process does not keep any directory in use that may be on a mounted file system (allowing it to be unmounted).
  6. Changing the umask to 0 to allow open(), creat(), and other operating system calls to provide their own permission masks and not to depend on the umask of the caller
  7. Closing all inherited files at the time of execution that are left open by the parent process, including file descriptors 0, 1 and 2 for the standard streams (stdin, stdout and stderr). Required files will be opened later.
  8. Using a logfile, the console, or /dev/null as stdin, stdout, and stderr

如果该进程由超级服务器守护程序(例如inetd,
启动或systemd)启动,则超级服务器守护程序将执行那些
用于process [5] [6] [7]的函数(不将旧$$$$ b转换为在systemd下运行并指定为Type = forking [7]和
multi-threaded的旧式守护程序除外) inetd [5]下的数据报服务器。)

If the process is started by a super-server daemon, such as inetd, launchd, or systemd, the super-server daemon will perform those functions for the process[5][6][7] (except for old-style daemons not converted to run under systemd and specified as Type=forking[7] and "multi-threaded" datagram servers under inetd[5]).




  1. 是否有步骤可以更改进程
    的父进程被守护?在我看来,这些步骤都没有吗?

  1. Is there a step there that changes the parent process of a process to be daemonized? It seems to me none of the steps does that?

守护进程时是否需要更改父进程?

Is changing parent process necessary when daemonize a process?

更改了流程的父流程(不一定要守护的
流程)
后,该流程可以与新父进程的控制
tty相关联? (问题的目的是让
看到保持与新父进程的
控制tty无关的进程是否是改变主机的父进程的必要条件。处理。)

After changing the parent process of a process (a process not necessarily to be daemonized), can the process be associated to the controlling tty of the new parent process? (The purpose of the question is to see whether "keeping a process disassociated from the the controlling tty of the new parent process" is a necessary condition of "changing the parent process of the process".)

请参阅我的相关问题 https://unix.stackexchange.com/questions/266565/daemonize-a-process-in-shell

谢谢。

推荐答案

Unix进程的父进程本身不能更改。创建守护程序的典型方法涉及到 fork 调用(它将创建将成为守护程序的进程)。然后,初始过程将退出,而新孤立的子进程将由 init 进程继承,该进程将成为新的父进程。在第4步中进行了处理。 init 唯一要做的就是等待所有孩子退出。 init 没有控制TTY,因此一旦被 init 继承,守护进程就无法与控制相关联。 TTY了。取消关联的主要原因是为了防止从TTY生成的信号(挂断和Control-C等)到达守护程序。

The parent of a Unix process can't be changed by the process itself. The typical method of creating a daemon involves a fork call (which creates the process that will become the daemon). The initial process then exits, and the newly-orphaned child process will be inherited by the init process which becomes it's new parent. That's handled in step 4. The only thing init will do is wait for all it's children to exit. init doesn't have a controlling TTY, so once inherited by init the daemon can't become associated with a controlling TTY anymore. The main reason to become disassociated is to prevent signals generated from the TTY (hangups and control-C's etc.) from getting to the daemon.

守护程序有两种方式通常运行:

There are two ways daemons are usually run:


  1. 从Shell脚本执行。该脚本在命令末尾使用& 运算符运行守护程序的可执行文件,以将守护程序置于后台,可能通过I / O重定向来设置守护程序的stdin, stdout和/或stderr,然后退出而没有父项的守护程序。从外壳运行可执行文件需要外壳在可执行文件的子进程中执行 fork ,然后执行 exec

  1. From a shell script. The script runs the daemon's executable with the & operator at the end of the command to put the daemon into the background, possibly with I/O redirection to set the daemon's stdin, stdout and/or stderr, and then exits leaving the daemon without a parent. Running an executable from the shell involves the shell doing a fork followed by an exec in the child process of the executable to be run.

守护程序可以自行守护。当使用该选项运行时,它会执行 fork ,然后在子进程中执行 exec 本身,并带有适当的一组论点。家长通常会在叉子之后离开,因为要求完成工作。如果不是,则子进程需要额外的 fork 来为其提供可以退出的父进程。注意:这就是为什么许多通常作为守护程序运行的程序可以直接运行而无需成为守护程序的原因,成为守护程序选项会导致子进程关闭stdin / stdout / stderr,然后仅执行 exec 是它自己的可执行文件,没有成为守护程序选项。

The daemon program has an option to daemonize itself. When run with that option it does a fork followed in the child process by an exec of itself with an appropriate set of arguments. The parent will normally exit after the fork since the work it's been asked to do is done. If it doesn't, the child process needs an extra fork to give it a parent that can exit. NB: this is why so many programs that normally run as daemons can be run directly without becoming a daemon, the "become a daemon" option causes the child process to close stdin/stdout/stderr and then just exec it's own executable without the "become a daemon" option.

这篇关于守护进程时是否需要更改父进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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