Ever started a long-running task in Linux only to have it abruptly terminated when you close your terminal window? That’s where the nohup
command comes in. Short for “no hangup”, nohup
ensures your commands and scripts continue execution even after you log out or your connection is interrupted.
Table of Contents
Understanding the Problem
By default, processes in Linux are tied to the terminal session that spawned them. When you close the terminal, the process receives a SIGHUP (Signal Hang UP) signal instructing it to terminate. This can be frustrating for tasks that take a long time to complete, such as data backups, file downloads, or scientific simulations.
Suppose, you are executing programs over SSH and if the connection drops, the session will be terminated, all the executed processes will stop, and you may face a huge accidental crisis. In such cases, running commands in the background can be very helpful to the user and this is where nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs the process even after logging out from the shell/terminal.
The nohup Solution
The nohup
command acts as a shield for your processes. Here’s how it works:
- Blocks SIGHUP: When you run a command with
nohup
, it intercepts the SIGHUP signal, preventing it from reaching the process. This allows the process to continue running even when the terminal session ends. - Redirects Output: By default,
nohup
redirects both standard output (stdout) and standard error (stderr) of the process to a file namednohup.out
in the current working directory. This file captures any information the process generates, allowing you to review it later.
nohup Command Syntax
nohup
command syntax is as follows
nohup COMMAND [ARGUMENTS]...
nohup OPTION
command
: The command you want to run in the background.arguments
: Any arguments needed by the command.option
: nohup command option
Important Considerations:
nohup
doesn’t magically provide resources for your process to run indefinitely. Ensure your process has the necessary resources (CPU, memory) available on the system.- While
nohup
prevents termination from SIGHUP, other signals like SIGKILL can still stop the process.
nohup command Examples
Let’s see some examples of nohup command
1. Checking the version of nohup
Check nohup
version by running below command
$ nohup --version
2. Starting a process using nohup in Foreground
If you want to keep your processes/jobs running, precede the command with nohup
as shown below. The jobs will still continue running in the shell and will not get killed upon exiting the shell or terminal.
$ nohup ./long_running_script.sh
3. Starting a process using nohup in Background
To run your processes/jobs in the background, the ‘&
’ symbol is appended at the end of the nohup command. After executing, it doesn’t return to the shell command prompt after running the command in the background. It can be brought back to the foreground with the fg command.
$ nohup ./long_running_script.sh &
&
: The ampersand (&) symbol tells the shell to run the command in the background.
4. nohup advanced options
- Specifying Output File: You can redirect the output to a different file using standard redirection operators (
>
,>>
). For example:
nohup ./long_running_script.sh > custom_output.log &
- Checking Process Status: Use the
jobs
command to see a list of background jobs, including those started withnohup
.
- Terminating a
nohup
Job: There are two ways to terminate anohup
job:- Use the
jobs
command to find the job ID and then usekill %job_id
. - Find the process ID (PID) of the
nohup
job using tools likeps -ef
and then usekill pid
.
- Use the
Conclusion
nohup
is a valuable tool for ensuring critical tasks continue running in Linux, even if your terminal session is interrupted. By understanding its functionality and limitations, you can effectively manage long-running processes on your Linux system.
Reference Links:-
- https://man7.org/linux/man-pages/man1/nohup.1.html
- https://docs.oracle.com/cd/E36784_01/html/E36870/nohup-1.html