LinuxCentOS/RHELCentOS/RHEL 7CentOS/RHEL 8Oracle SolarisSolaris 11Solaris 11.3Solaris 11.4

nohup Command with Examples

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.

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:

  1. 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.
  2. Redirects Output: By default, nohup redirects both standard output (stdout) and standard error (stderr) of the process to a file named nohup.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
nohup 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
image 8 -
image 9 -

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.

image 10 -

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 &
image 11 -
image 12 -
  • Checking Process Status: Use the jobs command to see a list of background jobs, including those started with nohup.
image 13 -
  • Terminating a nohup Job: There are two ways to terminate a nohup job:
    1. Use the jobs command to find the job ID and then use kill %job_id.
    2. Find the process ID (PID) of the nohup job using tools like ps -ef and then use kill pid.
image 14 -
kill %job_id
image 15 -
kill pid

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:-

5 1 vote
Article Rating

Prashanth Nimesh

I have passed B.Tech in Computer Science and currently working as a System Administrator with over 4 years of experience in the IT field. I am also the creator of the theGeeksHub website and its main contributor.

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back to top button