mirror of
https://github.com/jupyter/notebook.git
synced 2025-02-11 12:30:51 +08:00
NbserverStopApp: stop notebooks through cli - jupyter notebook stop <PORT>
This commit is contained in:
parent
b1407901c0
commit
cd3233ff4e
@ -16,6 +16,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import select
|
import select
|
||||||
@ -346,6 +347,45 @@ class NotebookPasswordApp(JupyterApp):
|
|||||||
self.log.info("Wrote hashed password to %s" % self.config_file)
|
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)]
|
||||||
|
if ports:
|
||||||
|
print("There is currently no server running on port {}.".format(self.port))
|
||||||
|
print("Ports currently in use:")
|
||||||
|
for port in ports: print("\t* {}".format(port))
|
||||||
|
else:
|
||||||
|
print("There are currently no running servers")
|
||||||
|
|
||||||
|
|
||||||
class NbserverListApp(JupyterApp):
|
class NbserverListApp(JupyterApp):
|
||||||
version = __version__
|
version = __version__
|
||||||
description="List currently running notebook servers."
|
description="List currently running notebook servers."
|
||||||
@ -449,6 +489,7 @@ class NotebookApp(JupyterApp):
|
|||||||
|
|
||||||
subcommands = dict(
|
subcommands = dict(
|
||||||
list=(NbserverListApp, NbserverListApp.description.splitlines()[0]),
|
list=(NbserverListApp, NbserverListApp.description.splitlines()[0]),
|
||||||
|
stop=(NbserverStopApp, NbserverStopApp.description.splitlines()[0]),
|
||||||
password=(NotebookPasswordApp, NotebookPasswordApp.description.splitlines()[0]),
|
password=(NotebookPasswordApp, NotebookPasswordApp.description.splitlines()[0]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user