Nohup

Last updated
nohup
Developer(s) Various open-source and commercial developers
Operating system Unix, Unix-like, IBM i
Platform Cross-platform
Type Command

nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.

Contents

Output that would normally go to the terminal goes to a file called nohup.out, if it has not already been redirected.

Use

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 I/O files (stdin, stdout, or stderr), they will still hang the terminal. [1] See Overcoming hanging, below.

nohup is often used in combination with the nice command to run processes on a lower priority.

$ nohup nice abcd &

Implementations

Some shells (e.g. bash) provide a shell builtin 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. [2] Another relevant bash option is shopt huponexit, which automatically sends the HUP signal to jobs when the shell is exiting normally. [3]

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. [4]

The nohup command has also been ported to the IBM i operating system. [5]

Overcoming hanging

Note that nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH 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). [6] [7] 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 dependent processes, such as when a pseudo-terminal has not been allocated. [8]

Alternatives

For example, the following invocation of screen will run somescript.sh in the background of a detached session:
$ screen -A -m -d -S somename ./somescript.sh &

Related Research Articles

A background process is a computer process that runs behind the scenes and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification. The background process usually is a child process created by a control process for processing a computing task. After creation, the child process will run on its own, performing the task independent of the control process, freeing the control process of performing that task.

<span class="mw-page-title-main">Fork bomb</span> Type of denial-of-service software attack

In computing, a fork bomb is a denial-of-service attack wherein a process continually replicates itself to deplete available system resources, slowing down or crashing the system due to resource starvation.

<span class="mw-page-title-main">Daemon (computing)</span> Computer program that runs as a background process

In 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. Traditionally, the process names of a daemon end with the letter d, for clarification that the process is in fact a daemon, and for differentiation between a daemon and a normal computer program. For example, syslogd is a daemon that implements system logging facility, and sshd is a daemon that serves incoming SSH connections.

time (Unix) Command in Unix and Unix-like operating systems

In computing, time is a command in Unix and Unix-like operating systems. It is used to determine the duration of execution of a particular command.

<span class="mw-page-title-main">GNU Screen</span> Terminal multiplexer software

GNU Screen is a terminal multiplexer, a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate login sessions inside a single terminal window, or detach and reattach sessions from a terminal. It is useful for dealing with multiple programs from a command line interface, and for separating programs from the session of the Unix shell that started the program, particularly so a remote process continues running even when the user is disconnected.

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.

In computing, kill is a command that is used in several popular operating systems to send signals to running processes.

On POSIX-compliant platforms, SIGHUP is a signal sent to a process when its controlling terminal is closed. It was originally designed to notify the process of a serial line drop. SIGHUP is a symbolic constant defined in the header file signal.h.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

fish (Unix shell) User-friendly interactive Unix shell

fish is a Unix shell with a focus on interactivity and usability. Fish is designed to give the user features by default, rather than by configuration. Fish is considered an exotic shell since it does not rigorously adhere to POSIX shell standards, at the discretion of the maintainers.

<span class="mw-page-title-main">GNOME Terminal</span> Terminal emulator from GNOME

GNOME Terminal is a terminal emulator for the GNOME desktop environment written by Havoc Pennington and others. Terminal emulators allow users to access a UNIX shell while remaining on their graphical desktop.

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.

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.

In Unix shells, wait is a command which pauses until execution of a background process has ended.

In Unix and Unix-like operating systems, job control refers to control of jobs by a shell, especially interactively, where a "job" is a shell's representation for a process group. Basic job control features are the suspending, resuming, or terminating of all processes in the job/process group; more advanced features can be performed by sending signals to the job. Job control is of particular interest in Unix due to its multiprocessing, and should be distinguished from job control generally, which is frequently applied to sequential execution.

In a Unix shell, the full stop called the dot command (.) is a command that evaluates commands in a computer file in the current execution context. In C Shell, a similar functionality is provided as the source command, and this name is seen in "extended" POSIX shells as well.

The restricted shell is a Unix shell that restricts some of the capabilities available to an interactive user session, or to a shell script, running within it. It is intended to provide an additional layer of security, but is insufficient to allow execution of entirely untrusted software. A restricted mode operation is found in the original Bourne shell and its later counterpart Bash, and in the KornShell. In some cases a restricted shell is used in conjunction with a chroot jail, in a further attempt to limit access to the system as a whole.

In the Unix shells ksh, bash, fish and zsh, the disown builtin command is used to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent to them if the parent shell receives it.

<span class="mw-page-title-main">Shellshock (software bug)</span> Security bug in the Unix Bash shell discovered in 2014

Shellshock, also known as Bashdoor, is a family of security bugs in the Unix Bash shell, the first of which was disclosed on 24 September 2014. Shellshock could enable an attacker to cause Bash to execute arbitrary commands and gain unauthorized access to many Internet-facing services, such as web servers, that use Bash to process requests.

References

  1. "Re: nohup/disown and logout". Zsh.org. 2005-02-07. Archived from the original on 2009-05-18. Retrieved 2009-06-10.
  2. Bash Reference Manual Archived 2010-12-03 at the Wayback Machine . Gnu.org. Retrieved on 2015-04-13.
  3. Bash Reference Manual Archived 2010-12-03 at the Wayback Machine . Gnu.org. Retrieved on 2015-04-13.
  4. IBM Knowledge Center Archived 2014-10-15 at the Wayback Machine . 01.ibm.com (2015-03-26). Retrieved on 2015-04-13.
  5. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM . Retrieved 2020-09-05.{{cite web}}: CS1 maint: url-status (link)
  6. "SSH Frequently Asked Questions". Snailbook.com. Archived from the original on 2009-01-22. Retrieved 2009-06-10.
  7. "OpenSSH FAQ". Openssh.com. 2005-09-20. Archived from the original on 2009-07-10. Retrieved 2009-06-10.
  8. "Bug 396 – sshd orphans processes when no pty allocated". Bugzilla.mindrot.org. Retrieved 2009-06-10.