Detect active `flyctl ssh console` connection to machine

I’d like to determine from within a Linux (Ubuntu) server running on Fly whether a flyctl ssh console connection is currently active to the machine running that server. How can I do that?

I could use that information to avoid shutting the server down when idle (it is configured to stop after 2 minutes withouth active connections to the webapp it provides).

You could hack this together, but it’s not something that’s “officially” supported haha

If you list the network connections established for /.fly/hallpass, you should be able to tell if anyone is connected (this counts ssh as well as sftp).

For example, you can use lsof -ai -p $(pidof hallpass) | grep ESTABLISHED to list all active connections (if you have net-tools and grep installed).

I can’t guarantee that this will always work, but it should work for now. Do with that what you will :slight_smile:

1 Like

Thanks for the workaround! Works fine so far.

The package I needed to install is named lsof, too, not net-tools.

Running the following script every 2nd minute as a cronjob is enough for my simple use case:

#!/bin/bash

if (( $(rstudio-server active-sessions | wc --lines) == 1 )) && (( $(lsof -ai -p "$(pidof hallpass)" | grep --count ESTABLISHED) == 0 )) ; then 
  rstudio-server stop && killall tired-proxy
fi

I currently run an RStudio Server[1] which I want to shut down immediately after the last active user quits their session. Thus I can’t directly rely on tired-proxy since it would mean a temporary internet connection loss would cause unsaved work to be lost. But I use it to be able to make the machine not reboot, i.e. I kill tired-proxy[2] when I want the machine to be stopped[3].

The deployed container uses the following ENTRYPOINT shell script:

#!/bin/bash

/rocker_scripts/experimental/batch_user_creation.sh && /init & cron & tired-proxy --port 8777 --origin http://localhost:8787 --wait-for-port 5 --idle-time 3603

  1. Based on a Rocker container image which uses s6-overlay v2.1.0.2 that in contrast to v3.x does not refuse to start when not PID 1. ↩︎

  2. When I try to terminate s6 via s6-svscanctl -t, the machine is stopped but immediately restarted. Thus I guess s6’ signals are not forwarded to flyd. ↩︎

  3. The tired-proxy idle time is set higher than RStudio’s session-timeout-kill-hours setting, effectively rendering it ineffective. ↩︎

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.