2

I have a remote server that I usually connect to with ssh and upon connection byobu starts automatically (via a line in .profile set up with the script byobu-enable). Now I want to set up a different workflow for when I connect to the same server to use remote jupyter notebooks. I'd like the remote server start up jupyter and then have ssh forward the jupyter port to my client machine. I added this to my local .ssh/config

Host remote-server-jupyter
    HostName      123.45.6.789
    User          pgcudahy
    LocalForward  8889 localhost:8889
    ServerAliveInterval 30
    ServerAliveCountMax 3
    RemoteCommand cd ~/Projects && jupyter notebook --no-browser --port=8889

The problem is that "RemoteCommand" interferes with byobu startup so that after connecting and running the command I'm left with a plain text shell and not a pretty multiplex screen. How can I get both byobu and remote commands on connection?

A big thing is that I don't want these commands to run on every connection, only if I specify that I want a certain workflow. Obviously I can connect to byobu and then run a script on the server to set up my workspace, but I'd like to wrap this all up into one automated command from the client. Even better would be to have separate profiles that not only run custom commands but set up a custom byobu workspace with multiple windows and different commands in each window.

2

The key to the answer was in this stack overflow question. Use the ssh -t flag to open an interactive pseudo-terminal. Then byobu new-session and byobu send-keys to pass commands to a byobu session.

First make a .ssh/config on the local machine to set up the ssh connection

Host remote-server-jupyter
    HostName      123.45.6.789
    User          pgcudahy
    LocalForward  8889 localhost:8889
    ServerAliveInterval 30
    ServerAliveCountMax 3

Then put a script in the home directory of the remote machine which sets up the byobu session. I need a script rather than a list of commands so that I can test to see if a "jupyter" session has already been created. I'll call it remote-jupyter-startup.sh

#!/bin/bash
# Test if there's a 'jupyter' session already set up
if [ -z "$(byobu list-sessions | grep jupyter)" ]
    # If not, then set up the session
    then
    # Create a new detached session named 'jupyter'
    byobu new-session -d -s jupyter
    # Pass a 'cd' command to the session, then 'C-m' to execute
    byobu send-keys -t jupyter 'cd ~/Projects' 'C-m'
    # Pass the 'jupyter' command to the session, then 'C-m' to execute
    byobu send-keys -t jupyter 'jupyter notebook --no-browser --port=8889' 'C-m'
    # Create a second window in the session
    byobu new-window -t jupyter:1
fi
# Attach to the session
byobu attach-session -t jupyter

Make it executable

chmod +x remote-jupyter-startup.sh

Now on the local machine I can run

ssh remote-server-jupyter -t "./remote-jupyter-startup.sh;"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.