Right way to kill notebook server on single-user machine

52 views
Skip to first unread message

Tevian Dray

unread,
Apr 17, 2018, 2:34:41 AM4/17/18
to sage-support
I'm a relatively new Sage user, trying Jupyter for the first time.  OS is Ubuntu 14.04; Sage version is 8.1.  Starting Jupyter from the command line (sage -n jupyter) works as expected, but so far as I can tell the only way to shut down the notebook server after closing the browser is via ctrl-c.  What if sage is not started from a terminal?  Or if stdout is redirected to a file?  For example, if I redirect stdout when starting sage, then send ctrl-c after closing the browser, the sage-notebook process continues to run -- presumably because I have no way to send a second ctrl-c.

I found this 10-year-old post asking essentially the same question:
but parts of it are clearly out of date.  However, the suggestion to use "kill -2" sounds promising, but on which process?  I only see two, one running sage-notebook, the other running sage-cleaner.  Or is there some other way?  "killall python" works, but presumably isn't a clean shutdown -- and I certainly wouldn't want to script such a sledgehammer.

It would be really nice if there were a provided script for shutting down the notebook server cleanly!

Henri Girard

unread,
Apr 17, 2018, 3:12:52 AM4/17/18
to sage-s...@googlegroups.com

exit

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Nils Bruin

unread,
Apr 25, 2018, 3:42:54 PM4/25/18
to sage-support
When you start up the jupyter notebook it writes some files describing its state. On Fedora, they end up in in /run/user/<UID>/jupyter. There's a file there nbserver-<PID>.json. In the file you'll find some basic data about the server, including the "pid", which also appears in the filename.

executing

kill -2 <pid>

twice in quick succession has the same result as pressing ctrl-C twice at the terminal where the server is running, so that shuts down the notebook cleanly.

Obviously there can be multiple notebook servers running, even under the same UID, so there can be multiple files there. You'll have to have a way to pick the right one.

Tevian Dray

unread,
May 3, 2018, 5:32:22 PM5/3/18
to sage-support
Finally had a chance to test this; yes it works -- although it is apparently possible to send the two kill commands too close together. Thank you very much.  Have to say it's a bit of a kluge, though -- the design assumption that notebooks will always be started in shell windows that stay open is surely flawed.

Nils Bruin

unread,
May 3, 2018, 5:47:51 PM5/3/18
to sage-support
On Thursday, May 3, 2018 at 2:32:22 PM UTC-7, Tevian Dray wrote:
Finally had a chance to test this; yes it works -- although it is apparently possible to send the two kill commands too close together. Thank you very much.  Have to say it's a bit of a kluge, though -- the design assumption that notebooks will always be started in shell windows that stay open is surely flawed.

Indeed, there seems to be an even righter way:

https://github.com/jupyter/notebook/issues/1950

apparently you can do

$ jupyter notebook list
<you will see port number>
$ jupyter notebook stop <port number>

When doing this with sage you'd have to make sure to run it through sage's jupyter, so something like

$ sage -sh -c "jupyter notebook list"
$ sage -sh -c "jupyter notebook stop <port number>"


Tevian Dray

unread,
May 3, 2018, 6:08:54 PM5/3/18
to sage-support
Nope -- or at least, not for me.  "jupyter notebook list" works fine, but "jupyter notebook stop 8888" returns an error indicating that the file stop can not be found...  Perhaps this feature has been dropped?  This approach also appears to be slow, presumably due to overhead when starting jupyter.
Reply all
Reply to author
Forward
0 new messages