Jupyter Actions via Command Line on Remote JupyterHub: Issues with Kernel Restart

Hello,

I would like to control my Jupyter actions via the command line on a remote JupyterHub instance. Specifically, I need to upload a notebook, launch a notebook, and stop a notebook.

I had considered using Jupyter Server for this purpose. However, when I try to restart a kernel using the REST API, it interrupts the kernel but does not restart it.

Do you have any suggestions on why this might be happening? Additionally, if you have any documentation, videos, or other resources that could assist me in this process, I would greatly appreciate it.

Thank you.

JupyterHub only starts/stops your singleuser server, i.e. jupyter-server/lab/notebook. Once it’s started you then interact with jupyter-server directly (all JupyterHub does is proxy your requests).

Thanks for the clarification and help. I actually not interested in controlling the hub but the singleuser server. I want restart a kernel that is already running on the server for example.

Untested for a while, and not validated in a remote setting, but:

1 Like

Since I want just use just 3 simple action, if I could be able to solve the issue and using directly the rest api I would prefer so to avoid a new dependency. But thanks you very much for the reference I will try to give a look since could make me better understand how to communicate with the server.

Can you share the request you are using to restart the kernel? It should be POST /api/kernels/{kernel_id}/restart. And also please share the logs from the server when you are making the request.

endpoint = f"{JUPYTER_URL}/api/kernels/{KERNEL_ID}/restart"
response = requests.post(endpoint, headers={"Authorization": f"Token {API_TOKEN}"})

My notebook

import time
time.sleep(60)

I would expect the kernel interrupt and rerun
while I just got the interruption (by looking at the cell output of the notebook over the browser)

Log:

[I 2024-11-22 13:17:26.677 ServerApp] Kernel restarted: KERNEL_ID

jupyter_server : 2.7.3

I do not know if the above allow you to easly reproduce it. If not please let me know and I will try to provide full script and versions.

restarting the kernel only restarts the kernel process. Executing code (the “Run All” in “Restart & Run All”) is separate, and requires sending execute requests for each cell of the notebook after restart has completed. Restart and Run All is a JupyterLab/notebook client feature, not part of the Server’s REST API.