sudo shutdown machine when a certain process is inactive for some time

So basically, I want to shut down a machine when a process displays inactivity for some time. This is in the context of some EC2 instances that I’m running for some users. They use some dataprocessing/analysis tools, some of which are long running. People aren’t always good about shutting things down when they’re done or including poweroff within their scripts/notebooks. Ideally i want some startup process that monitors the activity of another process, and when it is idle for say 2 hours, shuts the machine down.

If it helps to be specific, the process I’m referring to is jupyterlab