Skip to content

Commit fad1ce5

Browse files
dlukeskevin-bates
andcommitted
Improve login shell heuristics
Co-authored-by: Kevin Bates <[email protected]>
1 parent b152dd3 commit fad1ce5

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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)