Command Line

How to Keep Server Processes Running After Closing SSH

How to Keep Server Processes Running After Closing SSH

We’ve all experienced it: you’ve SSH-ed into a server and started a long-running task. Unexpectedly, it’s taking much longer than anticipated, and now you can’t close your laptop without interrupting the process. Fortunately, there’s a simple solution to close the terminal window without killing the ongoing task.

Quick and Easy Method

Follow these steps to keep your process running even after closing the terminal:

  1. Press Ctrl + Z to suspend the current process
  2. Type bg and press Enter to resume the process in the background
  3. Execute disown -ah to remove all jobs from the shell and make them ignore SIGHUP
  4. Type exit to safely close the terminal

Demonstration

To experiment with this technique, try the following:

In Terminal 1:

bash -c "sleep 10" & disown -ah && exit

Open a new terminal (Terminal 2) and check if the process is still running:

ps aux | grep sleep

You’ll see that sleep is still in the process list. After 10 seconds, it will complete as expected.

This method allows you to maintain server processes even when disconnecting from SSH, ensuring your long-running tasks continue uninterrupted.

You can learn more about it here and here.

Suggested Articles

Leave a Reply

Your email address will not be published. Required fields are marked *