GNU screen memo

The screen utility is a free terminal multiplexer for Linux (source):

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ANSI X3.64 (ISO 6429) and ISO 2022 standards (e.g., insert/delete line and support for multiple character sets). 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 in it (or the specified command) and then 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, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.

I used it at work to daemonize some processes, like this:

  • Detach a process:
screen -dmS processname daemon.sh
  • Kill every detached screens with a given name (via):
for session in $(screen -ls | grep processname | cut -d. -f1); do screen -S "${session}" -X quit; done
screen -ls | grep processname | cut -d. -f1 | xargs kill # more brutal and efficient
  • Access a detached process:
screen -ls
There are screens on:
        397.process1   (Detached)
        6179.process2    (Detached)
2 Sockets in /var/folders/hj/gcmcx07n2gbdyjd8mtqs96w00000gn/T/.screen.

We want to access to process1 to check if everything is ok, this is as simply as:

screen -r process1

And when the screen has focus, we detach it by using the CTRL+A then D shortcut. Nevertheless, if screen -ls shows the process as (Attached), the command to attach to it is the following:

screen -D -r process1
:sessionname newname
  • From the commandline
screen -X sessionname newname
screen -S 8890.foo -X sessionname bar
  • Log screen output to a file (screenlog.X)
  • screen -L sessionname to start a new session that will log to the current dir
  • While attached to the screen session, type CTRL+A then Shift+H or simply CTRL+A then H to log only the current screen

 Share!