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 #. Ref |
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
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
screen -S thenag
In the Command (From) Session, open a regular or optionally a screen session. Create and run this script as needed.
#!/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