How Nohup Command in Linux Allows Running Processes in Background

Try this guide with our instant dedicated server for as low as 40 Euros

nohup

You might have seen processes shutting down after a session ends. This is the normal Linux behavior that ensures that there are no orphan processes and that resources are used in the best possible way.

However, many users want some processes to continue running even when they exit their current session.

In this blog, we’ll present nohup as a simple solution to this challenge. We’ll demonstrate how to use this command and see how nohup adds value to the user’s experience.

Table Of Content

    1. The Benefits of the nohup Command
    2. What is NoHup Command in Linux
    3. Nohup Command Syntax
    4. Check the Version of the nohup Command
    5. Starting a Nohup Process
    6. Use Nohup to Run a Task in the Background
    7. How to View the nohup Command’s Output
    8. Conclusion
    9. FAQ

Let’s start with an introduction to the nohup command.

The Benefits of the nohup Command

The nohup command offers several benefits when you need to manage long-running processes in Linux systems. The list includes:

1. You can ensure a process keeps running even after you log off the machine or close the terminal.

2. Since the nohup command is immune to hangups and termination signals, you can rest easy that your process will continue to execute without interruption.

3. It is simple to send the output of nohup processes to a file for further analysis or later use.

4. nohup allows background processing that frees up the terminal for further activities.

5. System administrators may efficiently handle crucial activities without worrying about manually keeping the processes alive.

What is NoHup Command in Linux

In Linux, you may use the nohup command to run a command or a script in the background, even after you log out of the current session.

“nohup” means “no hangup” because it stops a specific command from being terminated when the terminal session ends.

When you run a command or script with the “nohup” option, the process is disconnected from the active session, the standard output and standard error are sent to a file named “nohup.out,” and SIGHUP (hangup) signal is not sent to the process. The signal “nohup” guarantees that the command continues to run even if the controlling terminal is closed or the SSH session expires.

Nohup Command Syntax

Nohup command has a pretty standard syntax in the following format:

nohup command arguments

OR

nohup options

Let’s further see how to use the nohup command.

Check the Version of the nohup Command

Let’s start by checking the nohup command’s version on your system. For this, use the following command:

nohup --version

As you can see in the following screenshot, the command displays the version number of the nohup command.

Check the version of Nohup Command in Linux

Starting a Nohup Process

Let’s see the nohup command in action.

You can use the nohup with any command you wish to keep running after you exit the shell or terminal. With nohup in the prefix, the job will continue to run.

Here’s an example:

# nohup ./hello.sh

Starting process through Nohup Command in Linux

The output of the hello.sh script will be directed and stored to the nohup.out file. You can verify this by using the cat command to check the contents of this file:

cat nohup.out

The following screenshot shows the impact of the nohup command. The output of hello.sh is stored in the nohup.out file.

cat nohup.out

If you wish, you can send the output to a different file with the following command:

nohup ./hello.sh > output.txt

Check the following screenshot to see the output of the command.

nohup ./hello.sh > output.txt

Use Nohup to Run a Task in the Background

Let’s now see how you can use the nohup command to run a process in the background.

Note that the ping command is used for demonstration purposes only. We strongly advise not to use it because it can consume resources and slow down system performance, especially when running in the background.

Use the & sign at the end of the command to launch the process in the background. To illustrate, we’ll ping google.com and run the process in the background.

nohup ping google.com &

Here’s the output of the command:

Run a task through Nohup Command in Linux

Now, use the pgrep command to find the process of the ping command when restarting the shell.

pgrep -a ping

As you can see, the process is still active even when we’ve exited the shell.

pgrep -a ping

Use the kill command followed by the process ID to stop or terminate the process associated with the ping command.

kill 1927228

The kill command stops the process, as shown below

Nohup kill command

How to View the nohup Command’s Output

Finally, let’s see how to view the output of the nohup command.

For this, open the output file chosen during the command execution to see the output produced by a nohup process. If you didn’t specify an output file, the output will be directed to the nohup.out file in the current directory.

You can use any standard text editor to check out this file. To demonstrate, I’ll open the file in the vi editor:

vi nohup.out

Since we used the ping command with the nohup command, you can see that the output file contains the ping records.

View Nohup Command in Linux output

Conclusion

The nohup command in Linux is a valuable tool for executing long-lasting processes that need to persist even after the user logs out or the terminal session ends.

It ensures uninterrupted execution and provides convenient options for redirecting output. By incorporating the nohup command into your workflow, you can efficiently manage critical processes and enhance system administration.

FAQ

Q: Can I run graphical applications with nohup?

A: The nohup command is mainly intended for command-line programs and scripts.

Q: How can I check the exit status of a nohup process?

A: Immediately following the nohup command, the $? the variable can be used to determine the exit state of a nohup process.

Q: Can I use nohup with a command that prompts for user input?

A: Yes, you may use interactive commands with nohup. However, remember that you won’t be able to engage directly with the input prompts because they will be cut off from the terminal.

Q: Is there a limit to the number of nohup processes I can run?

A: The number of nohup processes you may execute continuously depends on various variables, including system resources and administrator-set system constraints.

Q: Is there a way to resume a nohup process after logging back in?

A: No, you won’t be able to continue a nohup process once you log out of the system. You can examine its output file to learn more about the output.

Try this guide with our instant dedicated server for as low as 40 Euros