Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tech:linux:screen [2019/11/26 06:27] (current)
Line 1: Line 1:
 +====== Screen Usage ======
 +Practical screen usage notes
 +
 +===== Screen Commands =====
 +List of more important screen commands to effectively operate screen
 +
 +==== When NOT in screen ====
 +^  Command ^ Description ^
 +|screen -D | Power Detach - Close everything but keep the screens running in background. This is when you are done working! |
 +|screen -R | When loggin back into the system do this to get back to the screens |
 +|screen -d -r | Detach and Reattach. This is used when there are some screens stuck as attached but you are actually detached |
 +
 +==== When in screen ====
 +^  Command ^ Description ^
 +| Ctrl-a c | Create new screen terminal |
 +| Ctrl-a d | Detach from screen termial and go to main non screen terminal |
 +| Ctrl-a w | List all screen terminals |
 +| Ctrl-a :at "#"​ stuff "echo hello world\n"​ | Send commands to other screen windows at #. [[https://​www.gnu.org/​software/​screen/​manual/​screen.html#​Colon|Ref]] |
 +| C-a h, C-a C-h | Save the scroll back buffer ​ [[http://​web.mit.edu/​gnu/​doc/​html/​screen_17.html]] |
 +| C-a H | Begins/ends logging of the current window to the file `screenlog.n'​. Saves Color codes [[https://​stackoverflow.com/​questions/​18127728/​differences-between-screenlog-and-hardcopy-on-gnu-screen]] |
 +
 +From terminal you can see the Screen Name with ''​echo $STY''​. ​ Relevant if you are having multiple screen sessions.
 +
 +
 +==== When in or not in screen ====
 +^  Command ^ Description ^
 +|screen -D -RR| Power Detach, Reattach or create new screen if there is no screen running |
 +
 +
 +==== Sending commands to screen terminals ====
 +[[http://​www.softpanorama.org/​Utilities/​Screen/​tips.shtml|Reference - Scroll to scripting]]
 +
 +Script to send commands to screen. ​ steps
 +  * Create two sessions
 +    * One GNU screen session where all the scripting is directed **to**.  ​
 +    * Another sessions (can be screen if needed) where all the commands are sent **from**.
 +  * Opening a named screen session called ''​thenag''​ as below
 +
 +In Target Session, create a screen session named ''​thenag''​. If you want a different name you will also have to update the script to reflect this new name.
 +<code bash>
 +screen -S thenag
 +</​code>​
 +
 +In the Command (From) Session, open a regular or optionally a screen session. ​ Create and run this script as needed.
 +
 +Run the script with the name of the host file that basically contains a list of servers, the command to send and the sleep time (default 0.2 seconds) between screens. ​ The 1st time you want to run an ''​init''​ command followed by a ''​ssh''​ command. ​ These are special commands that will create screen windows and start an SSH session to each host.  When you are done you can send the ''​quit''​ command to terminate the screen session.
 +
 +<file bash rs>
 +#!/bin/bash
 +#
 +if [ $# -lt 2 ]; then
 +  echo "​Usage:​ $0 <Host file> <​Command>​ [sleep time]"
 +  echo "Host file contains the list of hosts to open screen session for"
 +  echo "​Command is the command to execute on each screen session"​
 +  echo "First time use init as Command to construct the screens"​
 +  exit 1
 +fi
 +#
 +# Get a list (say server names)
 +HOSTFILE=$1
 +# Create a counter for the screen windows
 +HOSTCMD=$2
 +# Number of seconds to sleep
 +if [ -z "​$3"​ ]; then
 +  SLEEPTIME="​0.2"​
 +else
 +  SLEEPTIME=$3
 +fi
 +#if ! [[ "​$SLEEPTIME"​ =~ ^[0-9]+$ ]]; then
 +#  SLEEPTIME=0
 +#fi
 +#echo $SLEEPTIME
 +#exit
 +# Define a name for the screen session.
 +SCRNAME='​thenag'​
 +NOW=$(date +"​%Y-%m-%d-%H-%M-%S"​)
 +TMPFILE="/​tmp/​${SCRNAME}-${NOW}.txt"​
 +#
 +if [ ! -f "​$HOSTFILE"​ ]; then
 +  echo "File: $HOSTFILE does not exist"
 +  exit 1
 +fi
 +#
 +if [ "​$HOSTCMD"​ == "​init"​ ]; then
 +  echo "​Initializing Screens ..."
 +fi
 +#
 +
 +if [ "​$HOSTCMD"​ == "​quit"​ ]; then
 +  echo "​Quitting Screens ..."
 +  screen -S $SCRNAME -X quit
 +  exit
 +fi
 +
 +declare sn integer
 +sn=0
 +cat $HOSTFILE | while read ServerName
 +do
 +  let sn++
 +  echo "​Processing $ServerName,​ Sequence $sn"
 +  case $HOSTCMD in
 +    init)
 +      # This creates a blank screen window for each instance
 +      screen -S $SCRNAME -X screen
 +      # Sleep may be required as sometimes commands get missed!
 +      sleep $SLEEPTIME
 +      screen -S $SCRNAME -p $sn -X stuff "echo $ServerName \n"
 +      ;;
 +    ssh)
 +      screen -S $SCRNAME -p $sn -X stuff "ssh ${ServerName}\n"​
 +      sleep $SLEEPTIME
 +      ;;
 +    tail)
 +      screen -S $SCRNAME -p $sn -X hardcopy $TMPFILE
 +      sleep $SLEEPTIME
 +      sed -e :a -e '/​^\n*$/​{$d;​N;​};/​\n$/​ba'​ $TMPFILE
 +      tail -3 $TMPFILE
 +      rm $TMPFILE
 +      echo "​--------------------------------"​
 +      ;;
 +    *)
 +      screen -S $SCRNAME -p $sn -X stuff "​${HOSTCMD} \n"
 +      sleep $SLEEPTIME
 +  esac
 +
 +done
 +</​file>​
 +
 +=== Command Examples ===
 +Below are the steps and command examples to execute commands across screens
 +
 +  * Setup host file - in this case we are calling it webs.txt
 +<file txt webs.txt>​
 +server80
 +server81
 +server82
 +server83
 +</​file>​
 +
 +  * Initialize the screen ​
 +<​code>​./​rs webs.txt init</​code>​
 +Output:
 +<​code>​
 +Initializing Screens ...
 +Processing server80, Sequence 1
 +Processing server81, Sequence 2
 +Processing server82, Sequence 3
 +Processing server83, Sequence 4
 +</​code>​
 +
 +  * SSH into each host
 +<​code>​./​rs webs.txt ssh</​code>​
 +Output: The same as above in init
 +
 +  * Run a custom command (in this case ''​sudo su - serviceacc''​)
 +<​code>​../​rs webs.txt "sudo su - serviceacc"</​code>​
 +Output: The same as above in init
 +
 +  * Just hit enter
 +<​code>​./​rs webs.txt ""</​code>​
 +
 +  * Tail the last 3 lines of the output on each session
 +<​code>​./​rs webs.txt tail</​code>​
 +Output: Will display Host Sequence and tail lines from the session
 +
 +  * Quit all screen sessions (opposite of init)
 +<​code>​./​rs webs.txt quit</​code>​
 +Output: Quitting Screens ...
 +
 +=== Original Script for reference ===
 +The hard-coded version of the script. Notice how you can pass passwords if required as well.
 +<code bash>
 +#!/bin/bash
 +#
 +# Get a list (say server names)
 +HOSTFILE=$1
 +# Create a counter for the screen windows
 +declare sn integer
 +sn=0
 +cat $HOSTFILE | while read ServerName
 +do
 +  let sn++
 +  echo "​$ServerName - $sn"
 +  # This creates a blank screen window for each instance
 +  screen -S thenag -X screen
 +  # Sleep may be required as sometimes commands get missed!
 +  sleep 1
 +  screen -S thenag -p $sn -X stuff "echo $ServerName \n"
 +  screen -S thenag -p $sn -X stuff "ssh ${ServerName}\n"​
 +  sleep 1
 +  screen -S thenag -p $sn -X stuff "ls -ld /tmp/files \n"
 +  screen -S thenag -p $sn -X stuff "sudo /bin/bash \n"
 +  sleep 1
 +  # You can even send password!!!
 +  screen -S thenag -p $sn -X stuff "​sudopasswordgoeshere\n"​
 +  sleep 1
 +  screen -S thenag -p $sn -X stuff "cd /tmp/files \n"
 +  screen -S thenag -p $sn -X stuff "​./​run_install \n"
 +done
 +# Let the install proceed on all screens
 +sleep 30
 +
 +# Check on the installs
 +sn=0
 +cat $HOSTFILE | while read ServerName
 +do
 +  let sn++
 +  echo "​$ServerName - $sn"
 +  screen -S thenag -p $sn -X stuff "echo $? \n"
 +  # Display last three lines of screen terminal
 +  screen -S thenag -p $sn -X hardcopy /tmp/s1.txt
 +  tail -3 /tmp/s1.txt
 +  rm /tmp/s1.txt
 +  echo "​--------------------------------"​
 +done
 +</​code>​
 +
 +
 +
 +===== Screen Resources =====
 +[[http://​www.gnu.org/​software/​screen/​|GNU Screen]]
 +
 +[[http://​www.gnu.org/​software/​screen/​manual/​screen.html|Screen User Manual]]
 +
  

QR Code
QR Code tech:linux:screen (generated for current page)