GNU Screen

Go to:

Introduction Top

If you need to work on remote computer, you may need to run commands that will run for some time. If you just run the command from the command line, you need that the SSH connection remains alive until the command will not finish, otherwise the command will be killed.

The most simple solution to avoid this problem is to use nohup:

user@remote:~$ nohup ./job args &
.
The nohup instruction allows ./job to run even if the SSH connection is detached. What you cannot do, however, is to reconnect and to interact with the shell to read the output or to check that ./job is working correctly, for example.

GNU Screen is a useful software that allows to detach from a remote shell and recover it at any time, even from a different computer. It is a full-screen window manager that allows to run multiple sessions, detach and attach to any of them, scroll back the history of each tab and run independent programs. The programs run also when the tabs are not active or when the Screen session is detached.

Basic usage Top

A new Screen session named "example" can be started using:

screen -S example
.
This command creates and attaches the user to the session "example". To detach the session from inside, the escape sequence ctrl+a, d is available. To detach the session if it is attached in a different shell, instead, one should run
screen -d example
.
The session "example" can be attached again using:
screen -r example
.

Once a session has been created and attached, each window works as a normal shell. If one needs more than a shell within the same Screen session, a new windows can be created using ctrl+a, c and it can be renamed with ctrl+a, A. The escape sequence ctrl+a, " shows the window list and allows to switch to one of them. To switch between windows, other commands are available:

The scrollback can be viewed entering the copy mode: ctrl+a, [ or ctrl+a, esc. The material copied in the copy mode can be pasted using ctrl+a, ].

It is possible to lock the screen session using ctrl+a, x, so that you have to insert your password to continue working.

The help, that contains a list of keybindings, can be seen using ctrl+a, ?.

 

The full GNU Screen manual is available here, but it is very long. A quick reference is here.

Customized settings Top

The Screen session can be customized using runtime commands, that are entered after the sequence ctrl+a, :.

It is also possible to customize the GNU Screen settings using a .screenrc, that it is read by default when running a new Screen session. A file with a different name can be loaded using the option -c:

screen -c filename
.

Here there is a an explained example of settings that I use in the .screenrc file, but that can be also used as runtime commands for Screen:

#create a tab running "top":
screen -t "top"
stuff "top^M"
#create a tab, open the "~/software" folder:
screen -t "software"
stuff "cd ~/software^M"
#create an empty tab:
screen -t "empty"

#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 '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'

#set 2500 lines of scrollback
defscrollback 2500

#disable startup message:
startup_message off

#autodetach the session on SIGHUP:
autodetach on

#utf8 encoding is default:
defutf8 on

#set default session name, useful to distinguish and attach to existing sessions
sessionname default