If you are building a robot, sensor platform, weather station then you don’t want the code to stop running when you disconnect from the terminal, andnohupis just the thing for you.In this how-to, we will learn how to usenohupvia a series of examples

There are times when you want to run a command, and leave it running even after you close a terminal window / SSH session. The simplest way is usingnohup(no hangups) which will run any command or executable script long after we close the terminal.

A person walking away as Linux code runs

Basic Nohup Use in Linux

At its most basic,nohupcan be used with only a single argument, the name of the script / command that we want to run. For example if we had a Bash script calledtest.shwe could run it as so.

If the script / command produces standard output, then that output is written tonohup.outand not the terminal. To check the output we can usetailto follow the updates made to the file.

Run Nohup in Linux

An Example of Nohup Use

We created a simple Bash script that every second prints a message to the screen, and updates the value stored in the variablen. Here is the code for that file, called test.sh.

1.Open a text editor and paste the example code.

Run Nohup in Linux

2.Save the file to your home directory as test.sh.

Get Tom’s Hardware’s best news and in-depth reviews, straight to your inbox.

3.Open a terminal and set test.sh to be executable.

Run Nohup in Linux

4.Test that test.sh is executable by running the command. If successful press CTRL + C to stop.

5.Use nohup and use the test.sh as an argument. Note that we need to use ./test.sh to tell the system where to find the script. The only output in the Terminal is “ nohup: ignoring input and appending output to ‘nohup.out’ “

Run Nohup in Linux

6.Open another terminal and use the tail command to follow the updates made to the nohup.out file.You should see “I keep counting” and a number counting upwards scroll across the screen.

7.Close the terminal running nohup. If asked to confirm closure, select Yes.The terminal running tail -f will keep running, showing that nohup is still running the script. We confirmed this by running htop and searching for test.sh.

Run Nohup in Linux

Redirecting the Nohup Output

Nohup has a limitation. By default it writes tonohup.out, so if we had two instances of nohup being used, they would both write to the same file.

To get around this we can use redirection to send the output to another file. So let’s usenohupalong with thepingcommand to ping one ofGoogle’s DNS servers, and save the output to GooglePing.txt.

Run Nohup in Linux

1.Open a terminal window.

2.Using the nohup command, use ping 8.8.8.8 as an argument.This will ping one of Google’s DNS servers every second, a useful connectivity test.

3.Open another terminal and use tail to follow the output of GooglePing.txt. Press CTRL+C when you are ready to stop the tailing the output.

Les Pounder

4.In the nohup terminal, press CTRL+C to stop the ping command.

Running a Nohup command in the background

Whennohupruns it grabs control of the terminal, effectively disabling it. But with a little more Linux magic we can put the command into the background. By placing the command into the background we can carry on using the terminal.

We’ll reuse the ping example to illustrate.

2.Using the nohup command, use ping 8.8.8.8 as an argument then add & to send the command to the background.Note that the output of running this command will be a process ID (PID). We will need this later.

3.Open another terminal and use tail to follow the output of GooglePing.txt.

4.In the nohup terminal press Enter to return to the prompt.The nohup command returns control to us, but the prompt is not present. By pressing Enter we can see the correct terminal prompt.

5.Use the kill command along with the process ID to terminate the background process.If the process is no longer required it is best practice to kill it in order to preserve resources. If we left this example to run it would continually ping Google’s DNS server until the computer was switched off.

Les Pounder is an associate editor at Tom’s Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program “Picademy”.