What is nohup?

πŸ“… March 17, 2024
“Help! I need to keep a program running after closing the terminal.”

There can be times when we connect to a remote system via SSH, for example, and we need to run a lengthy process on the remote system. However, we also might need to log out or close the connection, but we want the process to continue running.

Normally, the process terminates when the terminal is closed.

nohup is a way to keep a remote process running even after the terminal is closed.

Using nohup

nohup (No Hang Up) is a command built into the Bash shell that blocks the SIGHUP (Signal Hang Up) signal from reaching the running process. The process will run and then quit when it completes. To use it, just precede the command with nohup as a background task.

nohup ./myscript.sh &

Suppose myscript.sh is a custom script that we wrote. It exists on a remote system, and it performs a lengthy task. When we connect to the remote system via SSH, we can run myscript.sh on the remote system.

SSH login to a remote system allows us to run a program or script located on the remote system. Here, we have logged in from a terminal on the client. myscript.sh runs on the remote system.

myscript.sh will execute as long as the terminal on our client is open or until myscript.sh exists by itself when its execution is complete. But what if we need to log out of the remote system? myscript.sh would immediately terminate.

When the terminal is closed/SSH connection lost/SSH logged out, the script stops running on the remote system.

Continued Execution

So far, so good. What we have shown is normal operation. But what if we need to keep the script running on the remote system even after the SSH connection is lost?

This is where nohup enters the scene. By preceding the command/script name with nohup and setting it to run in the background, we can completely detach the script from the terminal so if the terminal is closed it will continue running on the remote system.

nohup ./myscript.sh &

The ampersand (&) runs a program in the background, so you will need to press Enter again to return to the command prompt.

Now, it works! We can set up a script to run on the remote system and it will continue to run until it completes even if we log out of the remote system.

“Why would I want to do this?”

Usually, for lengthy operations where you cannot remain logged in. A continuous login could pose a security risk, as one example scenario. The idea is to get the script started, and then log out since the script/process might take hours or days to complete.

nohup solves a very specific problem, so the average user will likely not need to implement it, but when the situation calls for it, nohup is a tremendous help.

What is nohup.out?

If the terminal is closed, stdout and stderr must go somewhere, so this output is logged into a file called nohup.out. nohup.out will be generated in the same directory from which nohup executes. Any output generated by the script/process will be logged to nohup.out so you can review it later in a text editor.

Caution! Depending upon the process, nohup.out can grow to many megabytes in size.

Suppressing nohup.out

“What if you do not want or care about the output of nohup?”

Then, suppress it like this:

nohup ./myscript.sh >/dev/null &

Remember to place the ampersand (&) last.

This suppress both stdout and stderr by redirecting both streams to /dev/null — the black hole of nothingness within Linux. The nohup.out file will not be created.

If you only want to suppress error messages but log stdout, then redirect stream 2 to /dev/null.

nohup ./myscript.sh 2>/dev/null &

Stopping a Remote Running Process

“What if you need to terminate a running process on a remote system, but you have already logged out?”

If you are still logged in, just use the jobs command and kill %1 (or whatever job ID is listed for the nohup command) to terminate it.

[1] 1386

jobs

[1]+ Running

kill %1

[1]+ Terminated

But if you log out and then log back in, this jobs technique will not work. jobs will return nothing even though the script is still running in the background. This is normal. We will need to kill the process by its process ID.

First, find the process ID. htop or BpyTOP are best for this. If not, you will need to know the command that myscript.sh is using. For example, if your remote script runs debmirror, then look for the PID (process ID) of debmirror. It will be located somewhere near the top of the process listing in a graphical program such as BpyTOP.

BpyTOP. Here is a partial listing of process running on a remote system. The highlighted yellow line is the nohup script running debmirror. Its PID is 1537.

We can also use the ps command:

ps aux | egrep debmirror

Either way, we need to obtain the PID, which is 1537 in this example. Yours will be different. Once we have the PID, just kill it.

kill 1537

Check BpyTOP or run the ps command again, and you will see that debmirror (for this example) is no longer listed. This means that the script/process has been terminated.

Conclusion

nohup is perfect for any scenario where you need to run a remote script and keep it running after logging out, so if you find yourself in this situation, nohup is your friend.

If you need to accomplish this and think that there might be no hope, remember that there is nohup.

Have fun!

 

,

  1. Leave a comment

Leave a comment