Nohup Command in Linux

Published on May 13, 2024

When you run a command or process on your Linux system, the process will exit when you close the terminal. However, it’s possible to keep the process running in your background instead of quitting it. The way to achieve this is to use the nohup command when executing the command. The nohup command stands for “no hang up,” and just as the name sounds, its aim is to ensure your process doesn’t hang up when you exit the terminal.

This post discusses the nohup command in detail. We will give examples of using the nohup command in Linux, and by the end of it, you will have a solid understanding of how and when to use it for your activities. Let’s get started.

How to Use the Nohup Command in Linux

Before we go to the examples of using the nohup command, let’s first check the version of the nohup command installed on our system.

Copy

This command is also a good way to verify that the nohup command-line utility is installed on your system.

Suppose you want to run the ls command to view the available files and directories on your system. You would execute the command as presented below.

However, when you exit your terminal, the process will automatically quit, and you won’t have a way of going back to it. However, when you run the same command but with the nohup command, you will notice that the job for the created process will be opened using nohup. Besides, the standard output won’t display the output for the running process.

Copy

By default, nohup will store the results of the running process in the nohup.out file.

When you display the contents of the output file, you will verify that the same output that would have been otherwise displayed on the standard output will now be saved in that file.

Suppose you wish to save the output of the process in another file instead of the default output file generated by the nohup command. Use the redirect symbol and add the name of the new output file for your process.

For instance, we’ve used ‘linuxmeta.txt’ as the output for our command, as demonstrated below.

Copy

To open the process using the nohup command and still run the process in the background, use the nohup command and add the & symbol.

For this example, let’s run the ping command to check the status of the linuxmeta.com website using the following command.

Copy

On another terminal window or tab, use the pgrep command with the -a option to get the process ID of the process running in the background.

Copy

Your output will give the PID and the name of the process.

With the PID, you can then kill the command using the kill command with the below syntax.

Copy

Alternatively, on the terminal where you’ve executed the command, press the enter key on your keyboard, and the process will immediately exit.

That’s how you use the nohup command in Linux. Feel free to check its man page for more explanations on the different options that you can combine when running the command.

Conclusion

The nohup command is an excellent way of running a process in Linux and running it in the background so that when you exit the terminal, your command will still continue executing. To recap, when you run a command with nohup, the output won’t be displayed on the standard output. Instead, the output will be saved in a file. Moreover, when you add the ‘&’ symbol with the nohup command, the process will execute in the background and you can kill it by retrieving its PID.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price