nohup Command in Linux with Examples

Every command in Linux starts a process at the time of its execution, which automatically gets terminated upon exiting the terminal. 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. 

Nohup Command

Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for terminating the process after closing/exiting the terminal. Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup.out file is used as the default file for stdout and stderr. If the output of the nohup command is redirected to some other file, nohup.out file is not generated. 

Nohup Command Syntax

The syntax for using the Nohup command is straightforward:

nohup command [options] &
  • `command`: Specifies the command or script that you want to execute.
  • `[options]`: Optional arguments or flags that modify the behavior of the command.
  • `&`: Placing an ampersand (&) at the end of the command instructs the shell to run the command in the background.

Working with nohup Command

  • Checking the version of Nohup
  • Starting a Process Using Nohup
  • Starting a Process in the Background Using Nohup
  • To run multiple commands in the background

Checking the version of Nohup

Checking the version of Nohup is a simple process. You can typically check the version of Nohup installed on your system by using the `--version` flag. Simply execute the following command:

nohup --version

Output:

Starting a Process Using Nohup

To start a process using Nohup, simply prepend the desired command with nohup. For example, if you want to execute a bash script named geekfile.py using Nohup, you would use the following command:

nohup bash geekfile.sh

Output:

To redirect the output to the output.txt file:

nohup bash geekfile.sh > output.txt

Output:

Starting a Process in the Background Using Nohup

To run the command in the background, the ‘&’ symbol is appended at the end of the 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 bash geekfile.sh &
fg

Output:

Note: The number within square brackets represents the job id and the number next to it is the process id.

To run multiple commands in the background

nohup command can be used to run multiple commands in the background.

nohup bash -c 'commands'

Example:

nohup bash -c 'cal && ls'

Output:

Here, the output will be by default stored in nohup.out. To redirect it, type:

nohup bash -c 'commands' > filename.txt

Example:

nohup bash -c 'cal && ls' > output.txt

Output:

nohup Command in Linux – FAQs

What is the nohup command in Linux, and what does it do?

The nohup command stands for “no hang up.” It allows you to run a command or shell script in such a way that it continues to run even after you log out or close the terminal. This prevents the command from being terminated when the terminal session ends.

How do I use the nohup command?

To use nohup, simply prepend it to the command you want to run. For example:

nohup command_to_run &

The & symbol at the end puts the command in the background, allowing you to continue using the terminal while the command runs.

Where does nohup send the output of the command?

By default, nohup redirects both standard output (stdout) and standard error (stderr) to a file named nohup.out in the current directory. You can specify a different output file if desired.

Can I use nohup with interactive commands or programs?

While nohup is typically used with non-interactive commands or scripts, you can use it with interactive commands. However, the behavior may be unpredictable, especially if the command expects input from the user. It’s generally recommended to avoid using nohup with interactive programs.

How can I check the status of a command running with nohup?

Since nohup runs the command in the background, you can use standard Linux tools like ps or top to check the status of the process. For example:

ps aux | grep <command_name>

This will show you information about the process, including its PID (Process ID), which you can then use to manage or monitor the command.

Conclusion

In Linux, when you run a command, it usually stops when you close the terminal. But with nohup, you can keep it running even after you’ve closed the terminal. This is super handy, especially if you’re running commands remotely and don’t want them to stop if your connection drops. nohup works by shielding your command from signals that would normally stop it. You just add nohup before your command, and it keeps going. You can also send the command’s output to a file. Just remember, it’s best for non-interactive tasks, and you can check on your command’s status using tools like ps. So, nohup is like a safety net for your commands, ensuring they keep chugging along even when you’re not watching.



Contact Us