Differences
This shows you the differences between two versions of the page.
— | tech:linux:screen [2025/04/01 11:25] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
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 "#" | ||
+ | | C-a h, C-a C-h | Save the scroll back buffer | ||
+ | | C-a H | Begins/ends logging of the current window to the file `screenlog.n' | ||
+ | |||
+ | From terminal you can see the Screen Name with '' | ||
+ | |||
+ | |||
+ | ==== 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:// | ||
+ | |||
+ | Script to send commands to screen. | ||
+ | * 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 '' | ||
+ | |||
+ | In Target Session, create a screen session named '' | ||
+ | <code bash> | ||
+ | screen -S thenag | ||
+ | </ | ||
+ | |||
+ | In the Command (From) Session, open a regular or optionally a screen session. | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <file bash rs> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | if [ $# -lt 2 ]; then | ||
+ | echo " | ||
+ | echo "Host file contains the list of hosts to open screen session for" | ||
+ | echo " | ||
+ | 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 " | ||
+ | SLEEPTIME=" | ||
+ | else | ||
+ | SLEEPTIME=$3 | ||
+ | fi | ||
+ | #if ! [[ " | ||
+ | # SLEEPTIME=0 | ||
+ | #fi | ||
+ | #echo $SLEEPTIME | ||
+ | #exit | ||
+ | # Define a name for the screen session. | ||
+ | SCRNAME=' | ||
+ | NOW=$(date +" | ||
+ | TMPFILE="/ | ||
+ | # | ||
+ | if [ ! -f " | ||
+ | echo "File: $HOSTFILE does not exist" | ||
+ | exit 1 | ||
+ | fi | ||
+ | # | ||
+ | if [ " | ||
+ | echo " | ||
+ | fi | ||
+ | # | ||
+ | |||
+ | if [ " | ||
+ | echo " | ||
+ | screen -S $SCRNAME -X quit | ||
+ | exit | ||
+ | fi | ||
+ | |||
+ | declare sn integer | ||
+ | sn=0 | ||
+ | cat $HOSTFILE | while read ServerName | ||
+ | do | ||
+ | let sn++ | ||
+ | echo " | ||
+ | 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 '/ | ||
+ | tail -3 $TMPFILE | ||
+ | rm $TMPFILE | ||
+ | echo " | ||
+ | ;; | ||
+ | *) | ||
+ | screen -S $SCRNAME -p $sn -X stuff " | ||
+ | sleep $SLEEPTIME | ||
+ | esac | ||
+ | |||
+ | done | ||
+ | </ | ||
+ | |||
+ | === 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 | ||
+ | </ | ||
+ | |||
+ | * Initialize the screen | ||
+ | < | ||
+ | Output: | ||
+ | < | ||
+ | Initializing Screens ... | ||
+ | Processing server80, Sequence 1 | ||
+ | Processing server81, Sequence 2 | ||
+ | Processing server82, Sequence 3 | ||
+ | Processing server83, Sequence 4 | ||
+ | </ | ||
+ | |||
+ | * SSH into each host | ||
+ | < | ||
+ | Output: The same as above in init | ||
+ | |||
+ | * Run a custom command (in this case '' | ||
+ | < | ||
+ | Output: The same as above in init | ||
+ | |||
+ | * Just hit enter | ||
+ | < | ||
+ | |||
+ | * Tail the last 3 lines of the output on each session | ||
+ | < | ||
+ | Output: Will display Host Sequence and tail lines from the session | ||
+ | |||
+ | * Quit all screen sessions (opposite of init) | ||
+ | < | ||
+ | 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 " | ||
+ | # 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 " | ||
+ | sleep 1 | ||
+ | screen -S thenag -p $sn -X stuff "cd /tmp/files \n" | ||
+ | screen -S thenag -p $sn -X stuff " | ||
+ | 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 " | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Screen Resources ===== | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | |||
+ | |||