Disclaimer

This blog is kind of my own personal work Notebook, and the processes below are only guaranteed to work for my specific configurations. So, use this info at your own risk, and please understand I won't be able to provide any help if you break something

Friday, December 8, 2023

Using 'screen' to detach your local computer from long-running processes on a remote server

After getting the error message "client_loop: send disconnect: Broken pipe", (which typically indicates a broken connection between the SSH client where you are running the command and the server), I started using screen, which is the perfect solution to manage long-running processes on a remote server that take a long time to complete, like data processing, backups, or maintenance scripts. 

screen or GNU screen is an utility that multiplexes a physical terminal between several processes, typically interactive shells. In my case screen allowed me to start a process remotely from my computer, disconnect from it, and then reconnect later, all without interrupting the process because my computer gets detached from it, so no more  broken connections issues. The whole process takes place uninterrupted at the server until you decide to reconnect and resume the session. Sounds like basic stuff I wish I had known earlier, but it's never too late!

Here's a step-by-step guide on how to use screen for this particular purpose:

Step 1: Connect to the Remote Server

First, use SSH or your preferred method to connect to the remote server where you want to run the process.

bash
ssh user@remote-server.com

Step 2: Install screen (if not already installed)

Make sure screen is installed on the server. You can usually install it using the package manager:

For Ubuntu/Debian systems:

bash
sudo apt-get install screen

For CentOS/RHEL systems:

bash
sudo yum install screen

Step 3: Start a New screen Session

Once connected to the server and screen is installed, start a new screen session:

bash
screen -S session_name

Replace session_name with a name that helps you identify the session later.

Step 4: Run Your Long-Running Process

Within the screen session, start your long-running process. For example:

bash
python long_running_script.py

or

bash
./rebuildall.php

Step 5: Detach from the screen Session

After starting the process, you can detach from the screen session and let the process run in the background. To detach, press:

  • Ctrl + A, then D

This key combination first tells screen you are about to give it a command (Ctrl + A) and then D tells it to detach.

Step 6: Disconnect from the Server

Now, you can safely disconnect from the server, and your process will continue to run in the screen session.

bash
exit

Step 7: Reconnect to the Server and Resume screen Session

Later, when you want to check on your process, reconnect to the server and use the following command to list your screen sessions:

bash
screen -ls

To reattach to your session, use:

bash
screen -r session_name

If you have only one detached session, screen -r will reattach to it directly.

Additional Tips

  • Multiple Sessions: You can have multiple screen sessions running at the same time. Give each one a unique name to identify them easily.
  • Session Commands: Within screen, Ctrl + A is the command prefix for various operations (like creating new windows, navigating between them, etc.).
  • Closing a Session: To close a session, simply exit the shell in the screen session, or you can detach and then type screen -X -S session_name quit.

Using screen in this way ensures that your long-running processes on a remote server are safe from network disruptions and can be easily managed without needing to keep a continuous connection.

There’s lots more to learn about screen at the Screen User’s Manual page.