Skip to content

Commit 0df10de

Browse files
authored
Merge pull request #5588 from dlukes/login-shell
Improve login shell heuristics
2 parents 3941004 + d157fa8 commit 0df10de

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

notebook/notebookapp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,10 @@ def _update_webapp_settings(self, change):
11601160
""")
11611161
)
11621162
terminado_settings = Dict(config=True,
1163-
help=_('Supply overrides for terminado. Currently only supports "shell_command".'))
1163+
help=_('Supply overrides for terminado. Currently only supports "shell_command". '
1164+
'On Unix, if "shell_command" is not provided, a non-login shell is launched '
1165+
"by default when the notebook server is connected to a terminal, a login "
1166+
"shell otherwise."))
11641167

11651168
cookie_options = Dict(config=True,
11661169
help=_("Extra keyword arguments to pass to `set_secure_cookie`."

notebook/terminal/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23

34
import terminado
45
from ..utils import check_version
@@ -18,16 +19,18 @@ def initialize(nb_app):
1819
default_shell = 'powershell.exe'
1920
else:
2021
default_shell = which('sh')
21-
shell = nb_app.terminado_settings.get('shell_command',
22-
[os.environ.get('SHELL') or default_shell]
23-
)
24-
# Enable login mode - to automatically source the /etc/profile
25-
# script, but only for non-nested shells; for nested shells, it's
26-
# superfluous and may even be harmful (e.g. on macOS, where login
27-
# shells invoke /usr/libexec/path_helper to add entries from
28-
# /etc/paths{,.d} to the PATH, reordering it in the process and
29-
# potentially overriding virtualenvs and other PATH modifications)
30-
if os.name != 'nt' and int(os.environ.get("SHLVL", 0)) < 1:
22+
shell_override = nb_app.terminado_settings.get('shell_command')
23+
shell = (
24+
[os.environ.get('SHELL') or default_shell]
25+
if shell_override is None
26+
else shell_override
27+
)
28+
# When the notebook server is not running in a terminal (e.g. when
29+
# it's launched by a JupyterHub spawner), it's likely that the user
30+
# environment hasn't been fully set up. In that case, run a login
31+
# shell to automatically source /etc/profile and the like, unless
32+
# the user has specifically set a preferred shell command.
33+
if os.name != 'nt' and shell_override is None and not sys.stdout.isatty():
3134
shell.append('-l')
3235
terminal_manager = nb_app.web_app.settings['terminal_manager'] = TerminalManager(
3336
shell_command=shell,

0 commit comments

Comments
 (0)