Using Gnu Screen To Pause And Resume Remote Sessions

GNU Screen is a terminal multiplexer that allows you to run multiple shell sessions in a single terminal window. With Screen, you can detach and reattach sessions, enabling you to keep processes running on a remote server even after disconnecting.

In this 6000-word guide, we will cover using GNU Screen to pause and resume sessions on remote Linux servers. Topics include:

  • Starting a Screen session
  • Reconnecting to detached sessions
  • Managing multiple sessions
  • Configuring session persistence
  • Sharing sessions between users
  • Advanced usage techniques

Starting a Screen Session

To start using Screen, you first need to install it on your server. Then you can launch a Screen session and run commands as you normally would. When you detach from the session, the programs you launched will keep running on the server.

Installing Screen

Screen comes pre-installed on most Linux distributions. To verify it is installed, run:

$ screen -v

If Screen is not already installed, you can install it from your distribution’s package manager.

On Debian/Ubuntu systems:

$ sudo apt update
$ sudo apt install screen

On RHEL/CentOS systems:

$ sudo yum update 
$ sudo yum install screen

With Screen now installed, you are ready to start your first session.

Running Screen

To start a Screen session, simply run:

$ screen

You will be greeted with a blank terminal Screen. You can now run commands as normal:

$ uptime
$ vim myfile.txt 
$ python myscript.py

As you run programs in Screen, there is no visible change from a normal terminal. But Screen is now managing this session in the background.

Detaching from a Screen Session

When you detach from a Screen session, the programs you started will continue running on the server. To detach, use the key sequence Ctrl+A Ctrl+D.

Alternatively, you can run:

$ screen -d

After detaching, your programs will keep running in the background. You can safely log out from the server now without disrupting them.

Reconnecting to a Screen Session

Picking up where you left off is seamless with Screen. You can reconnect from the same server or a different machine to attach back to your open sessions.

Listing Current Sessions

To see open Screen sessions on the server, use:

 
$ screen -ls

This will print a list of sessions, uniquely identified by a number and name. Make a note of the ID of the session you want to reattach.

Reattaching to a Session

With the session ID, reconnecting is simple. Run:

$ screen -r [session-id]

Replace [session-id] with your actual ID. You will now be reattached right where you left off, with all your processes still running.

That covers the basics of starting sessions and reconnecting. Next we will see how to manage multiple sessions.

Managing Multiple Sessions

Screen allows you to organize your work into multiple isolated sessions. You can name sessions, switch between them, and detach/reattach them individually.

Creating Named Sessions

By default, Screen identifies sessions by a number. To assign a name instead, start it with:

$ screen -S [name]

Now in the session list, your session will be identifiable by this custom name.

For example, to create two named sessions:

$ screen -S reports
$ screen -S development

Switching Between Sessions

With multiple sessions running, you can easily switch between them from within Screen using Ctrl+A Ctrl+N. This detaches from the current session and jumps to the next one in your list.

You can also switch to a specific session using its name or number by running:

  
$ screen -r [name/id]

This attaches you directly to your target session.

Screen Session Persistence

One benefit of Screen is being able to disconnect safely without disrupting your work. To ensure sessions persist reliably in the background, we need to configure a few settings.

Configuring Screen to Start on Login

To launch Screen automatically whenever you connect to a server via SSH, add this line to your ~/.bashrc user profile:

screen -raAd

Now when you log in, this will resume any detached sessions you have or create a new one.

Recovering Disconnected Sessions

If your SSH connection drops unexpectedly, your Screen session will become disconnected. To recover it, run:

  
$ screen -r -d

This will find your disconnected session on the server and reconnect.

With session recovery and auto start enabled, you can rely on Screen to keep your work persistent in the background.

Sharing Screen Sessions

In addition to using Screen solo, you can also share sessions with other users on the system.

Allowing Other Users to Connect

By default, only you can reconnect to your detached Screen sessions. To let another user view or control your session:

  1. Detach from the active session using Ctrl+A Ctrl+D
  2. Allow access by running:
    screen -x [session]

Now other users can attach using:

screen -r [owner/session]  

Joining Another User’s Session

To connect to an active Screen session belonging to another user:

$ screen -r -x [owner/session]

This will give you access to view or control their session. Collaborating via shared Screen sessions can be very useful.

Advanced Screen Usage

Up until now we have covered Screen’s basic session management capabilities. Here are some advanced features for customizing and extending sessions.

Custom Status Line

You can configure a custom status line to display information about your Screen session by adding this to ~/.screenrc:

hardstatus alwayslastline 
hardstatus string '%H (%S%?;%u%) %-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<'

This will show the session name, window number, and more.

Logging Output

To record all terminal output for a Screen session to a log file:

screen -L /var/log/screenlog-[session]

This is useful for auditing or debugging.

Sending Commands

You can send commands to a detached Screen session using:

$ screen -S [session] -X stuff [command]

For example, to send the ls command:

$ screen -S mysession -X stuff ls^M

This technique allows controlling sessions remotely.

Conclusion

That concludes our overview on using GNU Screen for session management on remote Linux servers. With the ability to pause and resume processes in the background, Screen is invaluable for tasks that outlive your SSH session.

We covered the core functionality like creating/reattaching sessions, persistence configurations, and managing multiple sessions. Additionally, we looked at some more advanced use cases like sharing sessions between users and sending commands to detached sessions.

Screen has many more capabilities and customization options beyond what was introduced here. The built-in manual pages provide more details on its comprehensive feature set. With this overview though, you should have a solid foundation for leveraging Screen to streamline your remote work.

Leave a Reply

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