0

This question has a good answer for how to put multiple command in an alias for bash.

But how would you do it in the case where you first need to ssh into a server, then do something like change a directory and then launch jupyter notebook?

I tried something like:

alias shortcut='ssh user@server -p 1234 -L 5678:localhost:91011; cd ~/somedir; jupyter notebook --ip=127.0.0.1

Maybe it's because my ssh requires me to type in a password, the last 2 commands aren't being executed.

Monica Heddneck
  • 2,967
  • 5
  • 41
  • 77

1 Answers1

1

There are some possible improvements for further convenience, if allowed by the system configuration.

If your need include executing a series of commands on the remote host, and you need to repeat this often, it's reasonable to put the commands in their own shell script and place it on the remote host.

For example in this case the script could be just

#!/bin/sh
cd ~/somedir && jupyter notebook --ip=127.0.0.1

Saving them in a file, add execution bit to it, and you can start the session like ssh user@server -p 1234 -L 5678:localhost:91011 path/to/script.sh

This is touched in this question but my preferred way is the low-score one about putting the script on remote -- I'd like to have each resource reside where they belong.

There's also the problem about what you want to do after starting the session. It seems the command is to start a server process that runs the Jupyter web service. If you just want to stay in the SSH session while monitoring the server, then the simple command should suffice. But if you want to keep the server in the background and log the output (and likely leave the SSH session for now) it's possible to run the server with nohup and redirect its output, by putting in the script something like

nohup jupyter notebook --ip="127.0.0.1" >> stdout.log 2>> stderr.log &
echo "$!" > jupyter-notebook.pid

The second command saves the PID in the file so it'll be easier to check or terminate it later without manually searching for the background process.

Cong Ma
  • 8,782
  • 2
  • 24
  • 46