Technical Resources for Technical People

Automatically launch Linux ‘screen’ command (plus advanced screen configuration)

July 7th, 2011 | Posted by techblog in Linux

As a system administrator, one of the most useful tools you can use is the Linux ‘screen’ command. This allows multiple virtual terminals to be accessed from one physical terminal (typically via remote SSH). Whether you call it tabs, screens, or windows – this is a must have tool. Even if you don’t need multiple tabs or windows in an SSH terminal, this will save you countless headaches in the event of a lost network connection to the physical machine as your process will continue to run. (Not to mention it’s easy to pick up where you left off in your last session).

The ‘screen’ package can be installed via apt-get or yum in the typical manner, though its base configuration needs a bit of sprucing up in order to make it easier to use and see what’s going on.

Skip to automatically launching screen section.

Creating a  SCREEN color profile

Shown below is a .screenrc file in the home directory of the user account we’ll be connecting to. It’s pretty basic and launches 4 terminal instances, 2 of which execute a command.

The caption shows the date/time and the hardstring contains the colors for active/inactive window tabs as well as the system load. You could also display the hostname or use different colors depending on what info you’d like displayed.

Here’s an example of the hardstatus string on another server:

hardstatus string "%{= kG}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..d}| %{..r}%l %{..d}| %{..Y}%D %d/%m %c "
# .screenrc
hardstatus on
hardstatus alwayslastline
startup_message off
msgwait 1

encoding UTF-8
attrcolor b ".I"

screen -t bash          1
screen -t bash          2
screen -t vnstat        3 vnstat -l
screen -t iftop         4 sudo /usr/sbin/iftop -f 'not port domain'

select 1

caption always "%{.0w} %= %{.0w}%D %d/%m %{.0w}%c "
hardstatus string "%{= kG}%-w%{.Gk}%n %t%{-}%+w %=%{+b ..d}|%{-b .km} %l "

Either create or modify the .screenrc file in your users home directory and run screen. # screen

Useful SCREEN Commands

In order to use screen you’ll have to remember a few simple commands which I’ll briefly touch on below. Though I do recommend checking out the man by running # man screen (‘Q’ should exit).

From a physical terminal
# screen -ls (lists available screens)
# screen -d (detaches an attached screen, can specify name/id if multiple available)
# screen -r (resumes a detached screen, can specify name/id if multiple available) 

From within screen
Ctrl+a, n (next window)
Ctrl+a, p (previous window)
Ctrl+a, 1 (go to a specific window #, replace accordingly)
Ctrl+a, c (create new window)
Ctrl+a, Shift+a (rename window)
Ctrl+a, d (detach screen session and return to physical term – preserving your screen session) 

Screen within a Screen
Ctrl+a, a, n (next window)
Ctrl+a, a, p (previous window)
… and you get the idea, Ctrl+a, then a, then the command.

Automatically launching and resuming SCREEN on connect

Without any color profiles it can be pretty easy to forget if you have screen running or not; and using screen at all times is highly recommended in case of network disconnect while you’re running a process or compiling. A simple modification to the .bashrc file is all we need.

Basically the code below checks if you’re on the physical machine itself or connecting remotely. When on the physical machine, you’ll get a terminal as usual without screen. This script assumes you’ll only be using 1 screen session

if [ $SSH_TTY ] && [ ! $WINDOW ]; then

    SCR=`screen -ls | grep pts | sort -n | awk '{print $2}'`
    RES=`screen -ls | grep pts | sort -n | awk '{print $1}'`

    if [ "$SCR" = "(Attached)" ]; then
      echo -e "Screen already running and attached!";
      screen -ls
      echo -e "Detatching and re-attaching...($RES)";
      screen -d $RES
      sleep 1
      screen -r $RES
    elif [ "$SCR" = "(Detached)" ]; then
      echo -e "Screen available and detached, resuming ($RES)";
      screen -r $RES
      echo -e "No screen available, starting new."


You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

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