GNU Screen Magic

GNU Screen is a terminal multiplexer. Simply put, it allows you to run multiple persistent sessions and each session can have multiple screen (shell) windows. These sessions will continue to run even if your SSH connection gets disconnected.

This setup is very useful for running processes that take a long time on a remote machine. One common example is training a machine learning model remotely without having to maintain a persistent SSH connection to the remote machine.

Another application is in robotics where the onboard robot machine is being accessed remotely from a workstation. Launching a locally run ROS node on the robot, for example, should not be affected by the SSH connection to the workstation. If you run roscore on the robot, you need a separate terminal to run the ROS node. Opening two shell sessions (terminals) can be accomplished using X11 forwarding to get an SSH connection that supports GUIs. A persistent SSH connection between the robot and workstation is however required for this. To overcome this constraint, you can create a screen session with multiple screen windows instead (and interact with the robot without X11 forwarding).

A working Example

To install GNU Screen, run: sudo apt-get install screen
To start screen, simply type screen in a terminal. To terminate the session type exit. To list all the screen sessions simply type screen -ls.

Since GNU Screen is a terminal multiplexer, you can run multiple screen sessions and each session can have multiple screen windows. Let’s look at a practical example of accessing a robot’s onboard computer from a remote workstation:

  1. Create a named session on the robot: screen -S launch (Here, launch is the name of the screen session. It can be anything).
    We now need to create some windows within this session. To create a window press Ctrl+A C (it will automatically be assigned a number in the range 0…9). Create a second window.
  2. To toggle between the 2 windows you can just press Ctrl+A 0 (for window 0) and Ctr+A 1 (for window 1). Let’s run roscore in window 0 and echo a ROS topic in window 1:
    (a) Ctrl+A 0: roscore
    (b) Ctrl+A 1: rostopic echo /[topic_name]
  3. To detach from a screen session simply type Ctrl+A D.
  4. You can now close the SSH session entirely, and the screen sessions will keep running. Even if your SSH connection is suddenly lost, the screen sessions will persist.
  5. Later when you want to reattach to the screen session, simple log back into the remote machine and type screen -r launch (Here, launch is the name previously given to the screen session).
  6. To close a screen window, simply type exit inside the window you want to close. To force close a screen window, press Ctrl+A K to kill the window (not recommended).

Common Screen commands

Screen windows are very powerful and can also be used to split a shell horizontally or vertically into regions or even run multiple processes remotely ( for example you might run Matlab and ROS at the same time) in different screen sessions. Screen windows can be managed using these commands:
Ctrl+A “ : List all windows
Ctrl+A A : Rename the current window
Ctrl+A N : Switch to next window in the list
Ctrl+A | : Split the screen vertically into regions
Ctrl+A S : Split the screen horizontally into regions
Ctrl+A X : Close the current region
Ctrl+A Q : Close all regions apart from the current one
Ctrl+A K : Kill current window (not recommended)
Ctrl+A \ : Kill all windows (not recommended)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s