SSH++

My research often has a computational component which means logging into one of my servers, kicking off a long running computation, waiting a few days and recovering the output. Here’s how I, inspired by Filippo Valsorda’s post, addressed some of the pain points with this sort thing.

To keep jobs running when logging off, I – like most people these days – use tmux. Here’s my .tmux.conf

new-session

# shell
set -g default-command /bin/zsh
set -g default-shell /bin/zsh

# source config file
bind r source-file ~/.tmux.conf

# history
set -g history-limit 20000

bind-key P command-prompt -p 'save history to filename:' -I '~/tmux.history' 'capture-pane -S -32768 ; save-buffer %1 ; delete-buffer'

The key line for what’s to follow is new-session, i.e. tmux will automatically create a new session if there isn’t one already when calling tmux attach.

To talk to the hosts, I – again, like many people these days – use mosh which handles connection drops gracefully. Thus, switching WiFi networks doesn’t mean my SSH session dies.

Combining the two above, I can call:

mosh $HOSTNAME -- tmux a

which mosh​es into $HOSTNAME and calls tmux a there. Since this is too much type I have a shell script

##!/usr/bin/env bash
mosh $1 -- tmux a

which I call motu. To make this behave as well as mosh and ssh in terms of tab completion, my .zshrc contains

compdef motu=ssh

which instructs ZSH to use completions from ssh for motu. Now, typing motu strombenzin logs me into my server strombenzin with either a tmux session being created or with being dropped into the previous one. To “log out” I do Ctrl-b d, i.e. I simply detach the tmux session.

Finally, being an emacs users, I create a bunch of emacs commands to do the same from within emacs

(dolist (server-name '("strombenzin"
                       …
                       …))

  (fset (intern (format "mosh/%s" server-name))
        (malb/make-toggle-shell
         (format "*mosh:%s*" server-name)
         `(progn
            (let ((vterm-shell (format "mosh %s -- tmux a" ,server-name)))
              (vterm (format "*mosh:%s*" server-name)))))))

which creates commands such as mosh/strombenzin (malb/make-toggle-shell is from my emacs configuration).

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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