======= Screen =======
===== Useful links and sources =====
[[https://www.gnu.org/software/screen/manual/screen.html|www.gnu.org/software/screen/manual/screen.html]]
[[http://www.tecmint.com/screen-command-examples-to-manage-linux-terminals/|www.tecmint.com/screen-command-examples-to-manage-linux-terminals/]]
===== Screen Overview =====
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.
Each virtual terminal provides the same functions of the usual terminal. There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows.
When screen is called, it creates a single window with a shell and the gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, etc. All windows run their programs completely independent of each other. **Screen allows p****rograms to continue to run when the window is currently not visible** and even when the whole screen session is detached from the user's terminal.
When a program terminates, screen (per default) kills the window that contained it. If this window was in the foreground, the display switches to the previously displayed window; if none are left, screen exits.
===== Create/Re-attach Screen Session =====
Create a screen with the parameters x and R
$ screen -xR
**-x** will attach to a not detached screen, for multi display mode.
**-R** wil resume the last detached screen session. If one doesn't exist, it will start a new session.
For more command parameters use
$ screen -help
While in the screen, you can press **CTRL + a**, then **?** to view all available commands
===== Leaving Screen Session =====
==== Detaching ====
Detaching the page will allow you to leave the session without quitting the process
press **CTRL + a**, then **d**
==== Killing ====
Killing a screen will also kill any process it is running
press **CTRL + a**, then **k**
===== Simple example =====
Open an SSH connection and **remember ** the host you are on
$ hostname
ramon-limon
Create a new screen instance
$ screen -xR
Create a bash script to run a job in the background, below is a script that will print every 10 seconds, 100 times.
$ vim test.sh
#!/bin/bash
echo `hostname`
COUNT=0
while [ $COUNT -lt 100 ]; do
let COUNT=COUNT+1
echo COUNT $COUNT
sleep 10
done
Open permissions on the .sh file
$ chmod 755 test.sh
Execute the script
$ test.sh
You should see the script running, now to test wether it continues after exit. Again, **//please remember the hostname// ** of the system you are running the job on. To exit the screen, press ctrl-a, then press d. You can re-enter the screen by again running the command
$ screen -xR
Now try closing out the shell and reconnecting to the host at [whatever hostname].ics.uci.edu. If you run the screen command again, you will find that your process is still running!
===== Creating New Terminals/Tabs =====
To create a tab inside of screen on the same server, Ctrl-a + c would create it. There is a way of creating a new tab but on a different host. Copy the follow contents to a file called 'sssh.sh':
#!/bin/sh
screen -t ${1} ssh -x ${1}
Next, if your shell is bash, edit your .bashrc file and add the following alias:
alias sssh="/home/icsuser/sssh.sh"
Make sure the path to the file is correct. Run "source .bashrc" to reload the file and run:
sssh openlab.ics.uci.edu
It will create a new screen tab on a new host.
===== .screenrc File =====
Here is an of a .screenrc file to put at the root of your home directory.
deflogin off
#screen -t Shell 0 bash
#screen -t Emacs 1 /usr/bin/emacs -nw
select 0
#vbell on
#change the hardstatus settings to give an window list at the bottom of the
#screen, with the time and date and with the current window highlighted
hardstatus alwayslastline
#hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK}%+Lw%< %{= kG}'
#hardstatus string '%{= kG}%-Lw%{= kW}%50> %n%f* %t%{= kG}%+Lw%< %{= kG}'
hardstatus string "%{= ky}%-Lw%{=r}%20>%n %t%{= ky}%+Lw %{= ky}%-=| %{= kw}%M%d %c%{-} %{=r} ${USER}@%H "
multiuser on
aclchg :window: +x select
attrcolor b ".I"
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
defbce "on"
===== Tips for large jobs =====
* Remember the hostname
* Don't have it running endlessly, have some sort of time constraint.
* Get updates on the job's progress. This can be done with email, logs, etc.