Tuesday, September 27, 2016

The power of GNU screen

The power of GNU screen


Ever feel like the whole world knows something really useful except you?  I went to a tech conference the other day and someone mentioned the screen command.  Wow...like where have you been all my life?!

In a nutshell: screen allows you to start a long-running process and then "detach" and shut down your computer while the command continues to run on the server.  Very cool.  Heres a quickstart guide:

perform initial configuration by opening ~/.screenrc and add the following:
# enable shell in screen
shell -$SHELL

# turn off flash notification
vbell off

# clear terminal when exit editor or screen
altscreen on

# set title
termcapinfo xterm*|rxvt*|kterm*|Eterm* hs_ts=E]0;:fs=07:ds=E]0;07
hardstatus string "%h"
hardstatus off

# set footer
caption always "%{= kw} %=%-w%{= bk}%50>%n %t%{-}%+w%<"

in terminal, verify screen is installed:
screen -v
in terminal, open a new session
screen
in screen (has purple box in footer), create new session
Ctrl+a c
in screen, switch to another session (where # refers to the session number 0, 1, 2, etc.)
Ctrl+a #
in screen, switch to next running session (use p for previous)
Ctrl+a n
in screen, list the running sessions
Ctrl+a "
in screen, detach from the current session
Ctrl+a d
in terminal, list the running sessions
screen -ls
in terminal, resume a running session
screen -r #####

P.S. You can change your hardstatus string value to a variety of things to display in your terminal title.  However, adding the user name is tricky.  If you use %h you get the default prompt, which likely contains some variation of user@domain, but if you want to create a custom title (say, server name ~ user name), then things get a bit harder.  Most guides will tell you to include ${USER} in your string, which does in fact work...unless you sudo su to a different user account.  Argh! - it still shows your original login username!  To fix this, add the following to each accounts ~/.bashrc or ~/.bash_profile file (where the pink text is your custom title):

if [ "$TERM" = "screen" ] ; then PS1=$PS1"[e]0;${HOSTNAME%%.*} ~ ${USER}a]" ; fi

P.P.S. A popular alternative to screen is tmux

Go to link download