如何通过命令行检查 Jupyter 活动笔记本

How to check for Jupyter active notebooks through command line

我有一个 AWS EMR 运行 Jupyterhub 版本 0.8.1+,我想检查是否有 运行 任何代码的活动笔记本。 我已经尝试了以下命令,但它们似乎没有输出我在这里寻找的内容,因为用户服务器始终是 运行 并且笔记本可以是 运行 而无需执行任何代码。

# only lists running servers and jovyan is always running.
sudo docker exec jupyterhub jupyter notebook list
# No useful information outputted
curl -k -i -H "Accept: application/json" "https://localhost:9443/api/sessions"
# always lists processes regardless of running notebooks
ps aux | grep ipykernel
# The last_activity only updates when a user creates a new file or folder in the ui.
curl -k https://localhost:9443/hub/api/users/$user -H "Authorization: token $admin_token" | jq -r .last_activity
curl -k https://localhost:9443/hub/api/users -H "Authorization: token $admin_token" | jq -r .last_activity

我关注此 AWS 博客以在终止集群之前检查整个 EMR 是否空闲,但他们似乎从未完全实施 jupyter 检查。 https://aws.amazon.com/blogs/big-data/optimize-amazon-emr-costs-with-idle-checks-and-automatic-resource-termination-using-advanced-amazon-cloudwatch-metrics-and-aws-lambda/ 引用的大部分文件可以在 Github https://github.com/septian-putra/emr-monitoring

中找到

要查看笔记本是“忙”还是“闲”,您可以运行 curl -ks https://localhost:9443/user/jovyan/api/kernels -H "Authorization: token ${admin_token}" 使用此命令,您需要做的就是将它放在带有 grep -q 的简单 if 语句中,以获得真假空闲值。

if [ $(curl -ks https://localhost:9443/user/jovyan/api/kernels -H "Authorization: token ${admin_token}" | grep -q "busy") ]; then
    JUPYTER_BUSY_NOTEBOOKS=1
else
    JUPYTER_BUSY_NOTEBOOKS=0
fi

(curl -ks 用于静默输出并忽略 ssl。jovyan 是我的管理员用户)

文档 https://jupyter-kernel-gateway.readthedocs.io/en/latest/websocket-mode.html#http-resources /api/sessions 看看也可能有用。