What is the difference between nohup and demon?

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

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

+45
unix daemon
Jun 05 '09 at 21:30
source share
4 answers

The nohup command is a poor person who runs a process like a daemon. As Bruno Ranshart noted, when you run a command in an interactive shell, it has a control terminal and will receive a SIGHUP (hang) signal when the control process (usually your login shell) is completed. The nohup command arranges input with /dev/null , and for output and errors, nohup.out , and for a program, ignore interrupts, stop signals and hangs. In fact, it still has the same control terminal - it simply ignores the terminal controls. Please note: if you want the process to run in the background, you must tell the shell to run it in the background - at least on Solaris (that is, you type " nohup sleep 20 & "; without an ampersand, the process runs synchronously in the foreground )

Typically, a process running through nohup is something that takes time, but which does not freeze, waiting for interaction from other sources.

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

When a process is completely dismounted, it goes through some steps that nohup code goes through; it reorders its inputs / outputs, so it is not connected to any terminal, it is not disconnected from the process group, it ignores the corresponding signals (which may mean that it does not ignore any signals, since there is no terminal to send any of the signals generated through the terminal). Typically, it expands once, and the parent completes successfully. A child process usually unfolds a second time after fixing its process group and session identifier, etc .; the baby will also come out. The grandson process is now standalone and will not appear in ps output for the terminal on which it was running.

You can watch Advanced Unix Programming , 3rd Edn by W Richard Stevens and Stephen A Rago, or Advanced Unix Programming, 2nd Edn by Marc J Rochkind for a discussion of demonization.

I have a daemonize program that will demonize a program that does not know how to self-determine it (correctly). It was written to fix defects in a program that was supposed to demonize itself, but did not work properly. Contact me if you want - see my profile.

+62
Jun 05 '09 at 22:36
source share

Become a demon

This link has a good list of steps that a process must go through to become a daemon: http://www.steve.org.uk/Reference/Unix/faq_2.html#SEC16

I cannot copy the list verbatim due to copyright (see the "About" section), but here's a summary:

  • fork (first time) - so we are not the leader of the group, and let the parent exit.
  • call setsid() - become the leader of a new session. This challenge only works if we are not the leader of the group. This new session does not have a control terminal.
  • fork (second time) - therefore, we are not the leader of the session (and therefore cannot restore the control terminal), and let the parent exit.
  • cd to the root directory - therefore, we do not prevent the removal of other directories.
  • set umask to the desired value (optional) - because we could inherit a mask that we did not want.
  • close stdin, stdout, stderr (or just open them again elsewhere).

poir

What nohup does:

  • If stdout and stderr are connected to the terminal, redirects them to nohup.out
  • ignores SIGHUP



Similarities and differences

Note that the only common actions are to redirect stdout and stderr. To be a demon, you don’t even have to ignore SIGHUP.

nohup does not require you to use ' & ' for the background of the process - this means that you can use ctrl-c to send SIGINT. The process still responds to keyboard input. It also does not change stdin automatically, so it is recommended that you do it yourself through " < /dev/null ".

Please do not confuse nohup with other functions commonly used with it (for example, with background information). The OP asked a question about nohup .

On practice

In terms of practicality, when you want to start a one-time long-term process that should continue when the shell completes, you will want to use nohup , but you will also want to combine it with the background and redirect stdin. One-time work should not be done by a daemon, but some of the daemon's properties can be useful when running nohup, for example, " cd / ".

Periodic scheduled tasks are best run through cron (or some other scheduler).

Demons are best suited for observing repetitive tasks that do not have a predictable start time. Usually there is no specific time for the daemon process to finish (it is clearly stopped by the user / other process or by shutting down the system). Often daemons are services that respond to applications (clients) or other conditions (for example, incoming data through an I / O device through unix select ()). Other demons interrogated the condition and performed the action in response.

Terminal Management Addendum

See this page . The summary is that the control terminal provides unlimited access to its stdin, stdout, stderr. Only one process group can access stdin. By default, background process groups can also write to stdout and stderr.

In addition, it seems that the keyboard signals sent to the terminal are sent only to the process group, which has as the control terminal.

+35
Jan 18 '12 at 18:05
source share

In UNIX variants, a process is associated with a terminal process (login shell). Therefore, when the terminal process ends, the process also terminates due to this connection. Nohup prevents the process from exiting when the terminal stops.

A daemon or daemon is a process that the system starts at startup, it runs until shutdown, and the user does not explicitly request it. Therefore, by definition, it is not part of the user's interaction, but belongs to the system.

If you have access to the system as a user, you can use nohup. If you are sysadmin, you can install the deamon process. This does not matter for the process.

+6
Jun 05 '09 at 22:05
source share

The daemon cannot be started, while nohup is initiated by the user.

-one
Aug 10 '10 at
source share



All Articles