1. Overview

When working with remote servers, especially over SSH, it’s common to use the nohup command. This command is meant to run processes that should persist even after the termination of the SSH session. However, disconnecting from the session while leaving the process running in the background can be tricky.

In this tutorial, we’ll explore the process of disconnecting after running a nohup command over SSH, ensuring our tasks continue running seamlessly.

2. Disconnecting from SSH Without Terminating nohup Processes

The nohup command stands for “no hang up”. We use it to run commands that should continue running even after the termination of the terminal session or if the user logs out. It’s particularly useful when executing time-consuming tasks or scripts on remote servers. This ensures that the command will keep running in the background, detached from the terminal session.

Once we’ve initiated a process with nohup, disconnecting from the SSH session without terminating the running command requires additional steps. Here are a few methods to achieve this.

2.1. Directly Running the Command

The most straightforward method involves directly running the desired command with the nohup prefix. For instance, if we want to run a script named long_process.sh, we would execute:

$ nohup ./long_process.sh &

Appending the ampersand symbol at the end runs the command in the background. The nohup ensures its persistence even after the termination of the SSH connection. This way, we can safely disconnect without worrying about our process being terminated.

2.2. Using the Here Document

Another elegant approach is to use a here document to feed the commands to the SSH session. This method allows us to input multiple commands, ensuring that the nohup process remains detached:

$ ssh user@remote-server << EOF
  nohup ./long_process.sh &
  exit
EOF

This script logs into the remote server, starts the desired process with nohup, and then exits the SSH session. The process will persist even after the disconnection.

2.3. Running Multiple Programs in the Background With a Single Command

When dealing with multiple processes, we can combine them into a single command, ensuring their independence from the SSH session:

$ nohup ./process1.sh & nohup ./process2.sh & nohup ./process3.sh &

This command initiates three processes concurrently. The nohup and ampersand combination guarantees their survival post-disconnection.

In addition to the conventional methods that we’ve discussed, there are alternative approaches that offer greater flexibility and control. This subsection explores three recommended techniques for achieving seamless disconnection while keeping nohup processes active.

3.1. Using disown

After running a command with nohup, we can use the disown command to remove the command from the shell’s job table. This prevents the shell from sending signals to the process even if the shell is closed:

$ our_command &
$ disown

This effectively removes the process from the shell’s control, allowing it to continue running even if the SSH session is terminated.

3.2. Using tmux

Another powerful approach is to use terminal multiplexers like tmux. This tool provides a virtual terminal that persists even after the SSH session is disconnected. This way, we can reconnect to the session later to check the status of our processes:

# Install tmux if not already installed
$ sudo apt-get install tmux

# Start a new tmux session
$ tmux

# Run the command
$ our_command

3.3. Using ssh With nohup

We’ll now explore two examples that demonstrate the effective use of ssh, nohup, and additional redirection for seamless remote execution.

The first example showcases a basic yet powerful command structure for initiating background processes on a remote node. Consider a real-world scenario where we need to start a data processing script or a server that should persist even after disconnecting from the remote server:

$ ssh user@remote-server "nohup python data_processing.py &"

The “ssh node” part of the command establishes an SSH connection to the remote server named “node.” We must replace “node” with the server hostname or IP address.

The “nohup sleep 10 &” is the command executed on the remote server. The nohup ensures the process continues running even if the SSH session is terminated. In this case, it’s a simple sleep 10 command, causing the shell to sleep for 10 seconds. The & at the end runs the command in the background, allowing the SSH session to be freed up for further operations.

The second example builds upon the first by introducing redirection to /dev/null for both standard output (stdout) and standard error (stderr):

$ ssh admin@server "nohup ./maintenance_script.sh 1>/dev/null 2>/dev/null &"

The “nohup sleep 10 1>/dev/null 2>/dev/null &” variant of the command suppresses any output or error messages produced by the sleep 10 command. The “1>/dev/null” redirects stdout to “>/dev/null”, and “2>/dev/null” redirects stderr to /dev/null.

4. Conclusion

In this article, we understood how running processes with nohup and disconnecting from SSH without terminating the processes is crucial for efficient remote server management. Whether we choose to use disown or tmux, these methods ensure that our long-running tasks continue uninterrupted, even if the SSH session is closed. Further, we experimented with these techniques to find the approach that best suits our workflow.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments