-
Notifications
You must be signed in to change notification settings - Fork 5.6k
NbserverStopApp: stop notebooks through cli - jupyter notebook stop <… #2388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cd3233f
219c762
b6ac73c
8b20dcc
277d000
ed4de77
b2f63b4
44df51a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import logging | ||
| import mimetypes | ||
| import os | ||
| import subprocess | ||
| import random | ||
| import re | ||
| import select | ||
|
|
@@ -346,6 +347,45 @@ def start(self): | |
| self.log.info("Wrote hashed password to %s" % self.config_file) | ||
|
|
||
|
|
||
| class NbserverStopApp(JupyterApp): | ||
| version = __version__ | ||
| description="Stop currently running notebook server for a given port" | ||
| kill_cmd='kill' | ||
| kill_signal='-3' | ||
|
|
||
| flags = dict( | ||
| json=({'NbserverStopApp': {'json': True}}, | ||
| "Produce machine-readable JSON output."), | ||
|
||
| ) | ||
|
|
||
| json = Bool(True, config=True, | ||
|
||
| help="If True, each line of output will be a JSON object with the " | ||
| "details from the server info fpyile.") | ||
|
|
||
| port = Integer(8888, config=True, | ||
| help="Port of the server to be killed. Default 8888") | ||
|
|
||
|
|
||
| def parse_command_line(self, argv=None): | ||
| super(NbserverStopApp, self).parse_command_line(argv) | ||
| if self.extra_args: | ||
| self.port=int(self.extra_args[0]) | ||
|
|
||
|
|
||
| def start(self): | ||
| server=next((server for server in list_running_servers(self.runtime_dir) if server.get('port')==self.port),None) | ||
|
||
| if server: | ||
| subprocess.Popen([self.kill_cmd,self.kill_signal,str(server.get('pid'))],stdout=subprocess.PIPE).communicate() | ||
|
||
| else: | ||
| ports=[s.get('port') for s in list_running_servers(self.runtime_dir)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can now reuse the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha - yeah, that was the very first thing i did, back before my initial commit, but In [1]: from notebook.notebookapp import *
In [2]: nbstop=NbserverStopApp()
In [3]: servers=list_running_servers(nbstop.runtime_dir)
In [4]: [s for s in servers]
Out[4]:
[{u'base_url': u'/',
u'hostname': u'localhost',
u'notebook_dir': u'/Users/brook/code/jupyter/notebook',
u'password': False,
u'pid': 27105,
u'port': 8888,
u'secure': False,
u'token': u'48019c94f11b76fa4690c1a7d431bf58e4b2fb2d0cd65ae5',
u'url': u'http://localhost:8888/'},
{u'base_url': u'/',
u'hostname': u'localhost',
u'notebook_dir': u'/Users/brook/code/jupyter/notebook',
u'password': False,
u'pid': 27213,
u'port': 8889,
u'secure': False,
u'token': u'4c1588c1139d214b9d1eb432fd000270e2645a226633fde5',
u'url': u'http://localhost:8889/'},
{u'base_url': u'/',
u'hostname': u'localhost',
u'notebook_dir': u'/Users/brook/code/jupyter/notebook',
u'password': False,
u'pid': 27238,
u'port': 8890,
u'secure': False,
u'token': u'db40ac85ddd5694cc4f67d9c4a18f4c066b63887f6d9fb9b',
u'url': u'http://localhost:8890/'}]
In [5]: [s for s in servers]
Out[5]: []
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, OK then. Thanks, merging :-) |
||
| if ports: | ||
| print("There is currently no server running on port {}.".format(self.port)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This print statement can be moved up one level, and only print ports below.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These print statements should also go on stderr, since they are error output.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It we move this above There is currently no server running on port 8888
There are currently no running serversIts seems the There are currently no running serversmight be better. Thoughts? |
||
| print("Ports currently in use:") | ||
| for port in ports: print("\t* {}".format(port)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch should have exit code 1 as well.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||
| else: | ||
| print("There are currently no running servers") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also end with |
||
|
|
||
|
|
||
| class NbserverListApp(JupyterApp): | ||
| version = __version__ | ||
| description="List currently running notebook servers." | ||
|
|
@@ -449,6 +489,7 @@ class NotebookApp(JupyterApp): | |
|
|
||
| subcommands = dict( | ||
| list=(NbserverListApp, NbserverListApp.description.splitlines()[0]), | ||
| stop=(NbserverStopApp, NbserverStopApp.description.splitlines()[0]), | ||
| password=(NotebookPasswordApp, NotebookPasswordApp.description.splitlines()[0]), | ||
| ) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
signal should be an integer from the
signalmodule, e.g.signal.SIGTERMUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usingsignal.SIGQUITsincesignal.SIGTERMrequests confirmationUPDATE: SIGQUIT works fine. See comment below.