Detect active `flyctl ssh console` connection to machine

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. ↩︎