Automatically reattaching to a screen session

One of screen's best features is its ability to detach from an entire session -- leaving all your windows and programs running -- and reattach later.

However, it's easy to forget to reattach when you log in, so it's helpful to configure your shell environment so you automatically reattach to your session when you log in. Fortunately, screen provides a few command line options that make it trivial to achieve this goal.

Before we configure our shell environment let's look at detaching in a little more detail. To detach a screen session, type C-a d. Because it's possible to run and detach from multiple sessions on the same host, there is a way to list all attached and detached sessions.

To view the list, type screen -ls. This will produce output similar to the following:

$ screen -ls

There are screens on:

1878.ttyp1.examplehost (Attached)

2983.ttyp2.examplehost (Detached)

2 Sockets in /tmp/uscreens/S-btanaka.

As you can see in this example, I'm running two screen sessions. One is attached, and the other is detached.

I commonly ssh to my host from a variety of different machines. If I want to automatically reattach to my screen session whenever I log in, there are two strategies I can employ. One is simple but effective. The other is slightly more advanced.

Let's look at the simple case first. The default behavior of screen-r is to reattach to a detached process. If it cannot because there are no detached sessions, or there is more than one detached session, or all sessions are already attached, then it simply reports the failure, lists the sessions and exits. This is sane and conservative behavior, so we can safely add it to $HOME/.profile (assuming you are using sh or zsh) or $HOME/.bash_profile (if you're using bash) as follows:

# Automatically reattach to any solitary, detached screen session.

# If reattachment is not possible, list sessions and prompt user.

screen -r

When you log on, screen -r will attempt to reattach. If it fails, you will see the reason and a session list, and you will be presented with a new shell prompt. Screen will not reattach, and it will not start a new session.

The more advanced case is a little trickier. It also tries to reattach to a solitary, detached session, but failing that, it takes further action. If it finds an attached session elsewhere, it detaches session and reattaches to your current terminal. This is very handy if you've, say, left work with your session attached and then want that session attached when you ssh in from home. Also, if there is no session to reattach, it creates a new session. To use this solution, add these lines instead:

# If possible, reattach to an existing session and detach that session

# elsewhere. If not possible, create a new session.

if [ -z "$STY" ]; then

exec screen -dR

fi

This story, "Automatically reattaching to a screen session" was originally published by LinuxWorld-(US).

Related:

Copyright © 2006 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022