We use the nohup command to keep a process from being killed when the kernel sends the SIGHUP signal to the process when the terminal is closed. In this article, we learn about this command through examples.
Table of contents.
In Linux, while we are running a process in the terminal and suddenly close it. The process is automatically terminated. In other cases, we might be connected to a remote host via SSH performing critical operations such as making backups, copying files, and suddenly encountering network connectivity issues.
Usually, when the network disconnects or we close a terminal session, a SIGHUP signal is sent to the current process to signal that a terminal hangup has occurred, and thus the currently executing process is terminated, this includes jobs running in the background initiated by the terminal.
To combat such situations, we run the processes as a background process using the nohup command. This command will continue to execute even after a terminal is closed or a network disconnects.
The command works by preventing the process from receiving a SIGHUP signal which is sent by the kernel when the terminal is closed.
What about the command output? Some processes produce output that is very useful and when we close a terminal we won't have a place to view the output. The nohup command redirects its output(stdin) and error(stderr) to an output file nohup.out. We can also specify output files, in this case, the output won't be sent to the nohup.out file.
We write nohup commands using the following syntax:
$ nohup COMMAND [ARG]...
To get started, let's start a process in the foreground and send its output to the output file nohup.out.
$ nohup command
Now when we concatenate the file nohup.out we have the output that is supposed to be displayed when the command was executed.
We can also redirect output to a specific file using I/O redirection:
$ nohup command > outputFile.txt
In the above example, we redirect output to a file named out.txt.
In Linux, we run commands in the background by appending a & sign at the end of a command. For example, to execute the ping command in the background we write:
$ nohup ping 188.8.131.52 &
We can bring the process back to the foreground by typing fg.
We can also close the current terminal session and start a new one. When we list the processes currently executing we will see the process we previously started. To list the process we write:
$ ps -aux | grep ping
To kill the process we first have to find its process id PID then execute the kill command:
$ kill PID
We are not limited to a single command, we can execute multiple commands in the background and send output to a file as follows:
$ nohup bash -c '[command 1] && [command 2] && [command 3]' > outputFile.txt
We use the nohup command to keep a process from being killed when the kernel sends the SIGHUP signal when the terminal is closed. In cases where we have network issues, power problems, or just want a process to proceed even if the computer logs out we use the nohup command.
For a comprehensive guide to the nohup command, we can execute the command $ man nohup in a terminal for the command manual.