Computing : Managing shell sessions with screen

Problem

If you have an unstable internet connection and you ssh session to your workgroup server is unstable, you probably would like to manage your remote shell session in a stable manner - so that you can reconnect to your running programs after you disconnected.

Solution: screen or tmux

The Linux tools screen or tmux are similar to window managers on your desktop but for Linux shells. After starting shells, you can add more 'tabs' and jump between these tabs with keyboard commands - all in the remote shell.

Also, if you disconnect the ssh session and reconnect, you can attach again to the screen or tmux session.

Warning

Do not use screen or tmux to start and leave heavy tasks running on workgroup servers! Please submit any real work to the batch system - the workgroup servers are shared with your colleagues and intended for testing, development etc. but not for the heavy work!

How to

There is an abundance of documentation on how to use screen or tmux, so you can find many tweaks and hints. For starting the most important commands are

  • start a screen session after login to a remote server with: screen
  • add a new tab to your screen session: ctrl-a c
  • jump to the next tab: ctrl-a n
  • jump to the previous tab: ctrl-a p
  • detach from the running session with: ctrl-a d
  • check, if you have running but detached screen sessions running on your current machine: screen -ls
  • re-attach to a detached screen session: screen -r   {+session id from screen -ls}

tmux works similar to screen (with ctrl-b starting any command) but is more powerful and can be tweaked further. For simplicity, screen is widely available with a basic set of options, tmux is your tool of choice, when you want to get most of your remote session manager.

Documentation

Starting points for more information