Nohup
Encyclopedia
nohup is a POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 command to ignore the HUP
SIGHUP
On POSIX-compliant platforms, SIGHUP is a signal sent to a process when its controlling terminal is closed....

 (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP
SIGHUP
On POSIX-compliant platforms, SIGHUP is a signal sent to a process when its controlling terminal is closed....

 (hangup) signal is by convention the way a terminal warns depending processes of logout.

nohup is most often used to run commands in the background as daemon
Daemon (computer software)
In Unix and other multitasking computer operating systems, a daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user...

s. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.
This command is very helpful when there is a need to run numerous batch jobs which are inter-dependent.

nohup is a low-level utility simply configuring a command to ignore a signal. As seen below, nohup is very far from being a full-featured batch system
Batch processing
Batch processing is execution of a series of programs on a computer without manual intervention.Batch jobs are set up so they can be run to completion without manual intervention, so all input data is preselected through scripts or command-line parameters...

 solving all the problems of running programs asynchronously.

Example

The first of the commands below starts the program abcd in the background in such a way that the subsequent logout does not stop it.

$ nohup abcd &
$ exit

Note that these methods prevent the process from being sent a 'stop' signal on logout, but if input/output is being received for these standard IO files (stdin, stdout, or stderr), they will still hang the terminal. See Overcoming Hanging, below.

nohup is often used in combination with the nice
Nice (Unix)
nice is a program found on Unix and Unix-like operating systems such as Linux. nice directly maps to a kernel call of the same name. For a given process, it changes the priority in the kernel's scheduler. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority...

 command to run processes on a lower priority.

$ nohup nice abcd &

Existing jobs, processes

Some shells (e.g. bash) provide a shell builtin
Shell builtin
In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute....

 that may be used to prevent SIGHUP being sent or propagated to existing jobs, even if they were not started with nohup. In bash, this can be obtained by using disown -h job; using the same builtin without arguments removes the job from the job table, which also implies that the job will not receive the signal. Before using disown on an active job, it should be stopped by Ctrl-Z, and continued in the background by the bg command. Another relevant bash option is shopt huponexit, which automatically sends the HUP signal to jobs when the shell is exiting normally.

The AIX and Solaris versions of nohup have a -p option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described disown builtin of bash, nohup -p accepts process IDs.

Overcoming hanging

Note - Nohuping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH
Secure Shell
Secure Shell is a network protocol for secure data communication, remote shell services or command execution and other secure network services between two networked computers that it connects via a secure channel over an insecure network: a server and a client...

 session. A different issue that often arises in this situation is that ssh is refusing to log off ("hangs"), since it refuses to lose any data from/to the background job(s). This problem can also be overcome by redirecting all three I/O streams:

nohup ./myprogram > foo.out 2> foo.err < /dev/null &


Also note that a closing SSH session does not always send a HUP signal to depending processes. Among others, this depends on whether a pseudo-terminal
Pseudo terminal
In some operating systems, including Unix, a pseudo terminal is a pseudo-device pair that provides a text terminal interface without an associated device, such as a virtual console, computer terminal or serial port...

 was allocated or not.

Alternatives

There are other ways to accomplish the ability to keep a program running after the user has been logged out. For example, the program could be run inside a GNU Screen
GNU Screen
GNU Screen is a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session...

-style screen multiplexer. The screen can be then detached. GNU Screen maintains the illusion that the user is always logged in, and allows the user to reattach at any time. This has the advantage of being able to continue to interact with the program once reattached (impossible with nohup alone). A related alternative would be to run the program in a 'detachable' graphical session such as that provided by VNC
Virtual Network Computing
In computing, Virtual Network Computing is a graphical desktop sharing system that uses the RFB protocol to remotely control another computer...

.

Another possibility would be to use setsid which will run a program in a new session. It is also possible to use 'dislocate' to achieve this effect.

Under Debian
Debian
Debian is a computer operating system composed of software packages released as free and open source software primarily under the GNU General Public License along with other free software licenses. Debian GNU/Linux, which includes the GNU OS tools and Linux kernel, is a popular and influential...

, it is possible to use the following command to daemonise a process: /sbin/start-stop-daemon

Another way to avoid the process being bound to a terminal is to have the at
At (Unix)
In Unix-like computer operating systems,the at commandis used to schedule commands to be executed once, at a particular time in the future....

daemon run it, as for example with echo command | at now.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK