Session Management for Cloud Shell and Codespaces

Eric van Wijk
2 min readOct 1, 2020

Did you ever run into the dreaded session time out message?

Speaks for itself..

Once you re-connect, you forgot where you were with whatever you were doing, and have to start from scratch. Well, with some preparation, there is a way to get back into your terminal session, and continue where you left off -even after having been disconnected.

Re-connect to a session from any client

The timeout is pertaining to your session, not to the underlying container running your Cloud Shell or Codespace. So, depending on how long you have been away, the container may still be alive and ready to accept new connections. Enter tmux, a utility that can be used to manage terminal sessions. In fact, tmux can do much more and act as a character mode window manager (like DESQview did on DOS way back, yes I’m that old). My prime use case for tmux is to address the timeout problem described above though.

To get started with tmux, you need to have it installed first. In Cloud Shell, it is available by default. However, in Codespaces you have to do that yourself:

sudo apt-get install tmux

Tmux manages sessions you create with it. To create a new session, type:

tmux new -s foo

This will create a new shell i.e. load profiles again. When you lose the connection, you can re-connect (provided the host is still running):

tmux attach-session -d -t foo

If you want to manually disconnect from a session (e.g. to concurrent processes), you can disconnect using Ctrl-D. Ctrl-K kills a session. Key bindings can be customized using .tmx.conf. This is mine:

This also formats a bottom status line that will remain visible in your session.

As I am a PowerShell user, I have created functions and aliases for the creation of sessions so I can customize the process:

Simply typing:

ct

creates or connects to a session, the function will create one if it didn’t exist yet.

As said, tmux is actually much more and provides a character mode window manager:

You can use this cheat sheet to start finding your away around.

Enjoy the productivity tmux provides!

--

--

Eric van Wijk

I’m a PM and ex-Cloud Solution Architect at Microsoft. You can also find me on GitHub here: https://github.com/geekzter. Opinions are my own.