Screen script for multi-user session or reminding you to create a screen on logon

A common problem when many people share large systems as the same user ( I know .. I know but anyways move on )  is that when you logon you might want to take over where someone else left off. Also sometimes you want to share a session with someone for supervision or just team experience.

Screen tool in linux is fantastic for this. I wrote this script to allow people to be reminded to have a screen session and if already there allow you to log on to the screen by either taking over the session or sharing it with the other user.

installation of screen is as easy as :

 

sudo yum install screen -y

or for you debbie penguins out there

 

sudo apt-get install screen -y

 

The script goes like this :

## Screen profile for user session sharing
## by David Saliba (copyleft) 2013 

#!/bin/bash

function greet {
 clear
 cat /etc/motd
 echo "Hostname:" `hostname `
 echo
 ifconfig | grep inet | egrep -v "inet6|localhost|127\.0\.0\.1"
 echo
 #  df -h /
 echo "Welcome ! #  No screen session  #"   
}

function newscreen {

 echo "Would you like to create a new session  ? (Y/n)"

  read -t 10 b
  if [[ $b == "N" || $b == "n" ]]; then
 { # Dummy if no just continue
  echo 
 }
 else
 {
  
  echo " Remember to use <CTRL> + A and then d to leave the screen session active or just disconnect "
  echo -n "Creating "
         sleep 1; echo -n "." ;sleep 1; echo -n "."; sleep 1; echo -n "."
  exec screen -S Workarea
 }
 fi
}

if [ -z "$STY" ]; then
 firstscreen=$(screen -list | grep "(" | cut -f 2 | head -n 1)
# echo $firstscreen

 if [ ! -z "$firstscreen" ]; then
 {
         echo "Found screen ($firstscreen).Do you want to jump on it (Y), or share the session (X)? (Default Y in 10s)"
         read -t 10 a
     if [[ $a == "N" || $a == "n" ]]; then
        {
         greet
        }
     elif [[ $a == "X" || $a == "x" ]]; then
        {
         echo -n "Joining "
         sleep 1; echo -n "." ;sleep 1; echo -n ".";
         exec screen -x $firstscreen
        }
     else
        {
         echo -n "Connecting and taking over"
         sleep 1; echo -n "." ;sleep 1; echo -n ".";
         exec screen -r -d $firstscreen
        }
     fi
 }
 else
 {
   greet   str3amuK
   newscreen
 }
 fi
fi

 

Save this script under /etc/profile.d/screen.sh or some other name you will recognize.

 

Sharing a screen SSH session

Sharing your Session

Assuming you start a screen session using

screen -S david

Ask your partner to connect using (assuming they are logged in using the same user account):

screen -x david

Now it’s simply magical. Multiple persons can type and work on the same terminal – it works best when you’re coordinating over the phone. Note that the dimensions of your terminal output will be the same for every user – to change it, press CtrlA and then capital F. This will make the screen output fit your current terminal size, and change it to that size for every connected user.

Detaching from a Screen Session

Important: To detach from the screen session so that you can resume later, simply close the window. If you typeexit, you’ll end up terminating the screen session and the processes running within.