Callbacks at the startup or shutdown of instances

Hello all,

In order to track the activity of our users (Jupyterhub on Kubernetes), we’d like to report the start and end (manually or due to cull settings) of sessions.

Are there any recommended mechanisms for attaching python or bash code to these events?

I considered writing two custom scripts and moving them to “/etc/rc3.d” and “/etc/rc0.d” to run at startup and shutdown, respectively. But I’m unsure if that’s the best option we have for Jupyterhub.

Kindly share any solutions you know useful for such features.
Thank you!

Edit: it seems possible to add “lifecycleHooks”, among which I saw an example of “postStart”. Is there also a “preStop” callback? If that’s the case then that should be what I need.

Hi Ash,

With dockerspawner I use c.Spawner.pre_spawn_hook and c.Spawner.post_stop_hook
Hope that helps,
Mark

1 Like

Thank you so much. I’ll be using these to implement the required functionality.

I’m also wondering if there’s a similar callback that could be launched asynchronously and remain running in the background. This would help send hardware usage stats for example to help with monitoring etc …

You could try c.JupyterHub.services, usually that’s used to cull idle notebooks, but you could see what you can do with it. If you’re just looking to monitor your pods you’re probably better off with pre-existing Kubernetes monitoring services.