Nohup Linux command with examples

Would you like to run a process in the background even after logging off your Linux PC? This session of Terminal Tuts teaches you the Nohup Linux command with examples.

Typically, in Linux, when your connection drops or user log out from the system, your session will terminate, and all the processes executed from the terminal will stop. If you want a program or command to keep running in the background even after log out or exit from the system, you may have to use the nohup command.

The nohup command will execute other programs or commands with its provided arguments and ignore all hangup signals. This command is useful, especially when connected to your server via SSH, and want the program or command to keep running in the background even after disconnecting from the server.

Nohup Linux Command

Syntax:

nohup COMMAND [ARGS]

or

nohup options

To find help regarding the nohup command:

nohup --help

To find the version information of nohup, use the below command:

nohup --version

1. Running Command in Foreground

By default, nohum command runs in the foreground and redirects the output to the ‘nohup.output’ file. This file will be created in the current working directory. If the user does not have permission, it will be created in the user’s home directory.

$ nohup ls

Example:

running nohup in foreground
running nohup in foreground

2. Running the Command in Background

Using the nohup command in the foreground has a drawback that you cannot interact with the terminal until command execution finishes. So to avoid this, we will run the command in the background like this

$ nohup ping fosslinux.com

Example:

running nohup in background
running nohup in background

You can see in the above example all the command output will be appended to the nohup.out file. You can view this file using the ‘cat nohup’ command in the Terminal.

[1] 2233

Where [1] is Job ID, and 2233 is the (PID) of the background process. You can use this Process ID to kill the background process:

$ kill 2233

The above command will kill the background process.

3. Redirecting Output to a File

By default, the nohup command writes output to the nohup.out file. You can define your own output file as well using standard shell redirection.

$ nohup ls > list.out

Example:

nohup custom output file
nohup custom output file

You can also create separate files for output and error using the following command-line.

nohup ls > list.out 2 > list.err

4. Running Multiple Commands

You can also run multiple commands with nohup.  In the below example, mkdir, bash, and ls commands are running in the background.

nohup  bash -c 'mkdir fossDir && ls'

Example:

nohup running multiple commands
nohup running multiple commands

Conclusion

In this tutorial, the uses of the nohup command are explained with straightforward examples, and we hope that now you can understand the usefulness of this command very well. Please feel free to leave a comment.

Habib Ahmed
He is a professional writer with more than seven years of experience in the field. He also enjoys technical writing/ manuals, white papers, and scientific presentations.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,241FansLike
394FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.
The wait is finally over (almost) for all you Ubuntu fans out there. The latest version of Ubuntu, 20.10 codenamed "Groovy Gorilla," is currently available in the beta version. I have tested out the distro myself, and it is stable enough to take out for a spin.

What’s New in Ubuntu 20.04 LTS “Focal Fossa”

Ubuntu 20.04 LTS is out now! This is the successor to Ubuntu 18.04, which was released in April 2018 and still has three years of official support left. Code-named Focal Fossa, the new edition has all the bells and whistles included in it, making it one of the best Ubuntu releases yet. Let's find out more.

10 ways to use the Chown command with examples

Any file in a Linux system belongs to a particular owner(s) and a group. It is one of the many features that maintain security in Linux systems. For example; If a file belongs to say UserA, then UserB cannot manipulate the data depending on any additional permissions set unless the user has permission to do that.

Removing the Virus from a Windows PC with a Ubuntu Live USB drive

In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.

The 5 Best Open Source Password Managers

It won't be wrong to say that managing passwords on your own could be a tad tricky, especially if you're frequently registering on new websites. Although your web browser's built-in password manager could do the trick, your passwords could still come into jeopardy in case you log in to your main account from another computer and forget to log out.