nohup 和守护进程有什么区别? [英] What's the difference between nohup and a daemon?

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

问题描述

将脚本作为守护程序运行与使用 nohup 相比有何影响?

What are the implications of running a script as a daemon versus using nohup?

我知道在 fork 进程等方面有什么不同,但这对我的脚本有什么影响?

I know what the difference is in terms of forking processes etc., but what impact does that have on my script?

推荐答案

nohup 命令是穷人将进程作为守护进程运行的方式.正如 Bruno Ranschaert 所指出的,当您在交互式 shell 中运行命令时,它有一个控制终端,并且在控制进程(通常是您的登录 shell)退出时会收到一个 SIGHUP(挂断)信号.nohup 命令安排输入来自 /dev/null,并将输出和错误都转到 nohup.out,并为忽略中断、退出信号和挂断的程序.它实际上仍然具有相同的控制终端——它只是忽略了终端控制.请注意,如果您希望该进程在后台运行,则必须告诉 shell 在后台运行它 - 至少在 Solaris 上(即,您键入 'nohup sleep 20 &'; 没有&符号,进程在前台同步运行).

The nohup command is the poor man's way of running a process as a daemon. As Bruno Ranschaert noted, when you run a command in an interactive shell, it has a controlling terminal and will receive a SIGHUP (hangup) signal when the controlling process (typically your login shell) exits. The nohup command arranges for input to come from /dev/null, and for both output and errors to go to nohup.out, and for the program to ignore interrupts, quit signals, and hangups. It actually still has the same controlling terminal - it just ignores the terminals controls. Note that if you want the process to run in the background, you have to tell the shell to run it in the background - at least on Solaris (that is, you type 'nohup sleep 20 &'; without the ampersand, the process runs synchronously in the foreground).

通常,通过 nohup 运行的进程需要时间,但不会等待来自其他地方的交互.

Typically, a process run via nohup is something that takes time, but which does not hang around waiting for interaction from elsewhere.

通常(这意味着如果你努力尝试,你可以找到这些规则的例外),守护进程是潜伏在后台的东西,与任何终端断开连接,但等待响应某种输入.网络守护进程等待连接请求或 UDP 消息通过网络到达,执行适当的工作并再次发送响应.例如,可以考虑 Web 服务器或 DBMS.

Typically (which means if you try hard, you can find exceptions to these rules), a daemon process is something which lurks in the background, disconnected from any terminal, but waiting to respond to some input of some sort. Network daemons wait for connection requests or UDP messages to arrive over the network, do the appropriate work and send a response back again. Think of a web server, for example, or a DBMS.

当一个进程完全守护自己时,它会经历nohup代码所经历的一些步骤;它重新排列它的 I/O,因此它不连接到任何终端,将自身与进程组分离,忽略适当的信号(这可能意味着它不会忽略任何信号,因为没有终端向它发送任何生成的信号通过终端).通常,它 fork 一次,并且父级成功退出.子进程通常会在修复其进程组和会话 ID 等之后进行第二次 fork;然后孩子也退出了.孙子进程现在是自治的,不会出现在启动它的终端的 ps 输出中.

When a process fully daemonizes itself, it goes through some of the steps that the nohup code goes through; it rearranges its I/O so it is not connected to any terminal, detaches itself from the process group, ignores appropriate signals (which might mean it doesn't ignore any signals, since there is no terminal to send it any of the signals generated via a terminal). Typically, it forks once, and the parent exits successfully. The child process usually forks a second time, after fixing its process group and session ID and so on; the child then exits too. The grandchild process is now autonomous and won't show up in the ps output for the the terminal where it was launched.

可以看Unix环境中的高级编程,第3Edn 作者:W Richard Stevens 和 Stephen A Rago,或在 Advanced Unix Programming, 2nd Edn,作者是 Marc J Rochkind,用于讨论守护进程.

You can look at Advanced Programming in the Unix Environment, 3rd Edn by W Richard Stevens and Stephen A Rago, or at Advanced Unix Programming, 2nd Edn by Marc J Rochkind for discussions of daemonization.

我有一个程序 daemonize 它将守护一个不知道如何(正确)守护自己的程序.编写它是为了解决程序中的缺陷,该程序应该自我守护,但没有正确完成工作.如果需要,请联系我 - 查看我的个人资料.

I have a program daemonize which will daemonize a program that doesn't know how to daemonize itself (properly). It was written to work around the defects in a program which was supposed to daemonize itself but didn't do the job properly. Contact me if you want it - see my profile.

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

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