Run application if and only if SSH Tunneling is active on port X

I am running a code-server on my remote Ubuntu machine and right now my method for starting it up is to

  1. ssh -t -t user@server "code-server" on one terminal (the -t -t I got from this)
  2. ssh -N -L 8443:127.0.0.1:8443 user@server on a second terminal
  3. Open the application on the browser

Then when I want to close it, I just close both terminals. My question is if would be possible to do this using only one connection, currently I have to authenticate twice because this requires two ssh sessions, one for starting the application and sending the KILL when closed and another one for the tunneling.

Asked By: Mathias Sven

||

I’ve tested the following and it seems to work:

ssh -t -t -L 8443:localhost:8443 user@server "sleep 3"

Connection to server closed.

it waits for 3 seconds and then closes the SSH session. I’d say you can put your application in the quotes instead of sleep 3 or remove the whole "sleep 3" and that would give you interactive shell.

Answered By: Roman Spiak
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.