Ctrl-C 和 SSH 命令

huangapple go评论60阅读模式
英文:

Ctrl-C and SSH command

问题

以下是您要翻译的内容:

"I am trying to make an alias to easily run Jupyter on a remote machine. To this end, I concocted this command:

ssh -L 5542:localhost:5542 remote
'cd ipython && .direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542'

This works perfectly... except for one thing: Ctrl-C in the terminal does not seem to reach the jupyter-notebook process (which asks for two Ctrl-C presses to shut down cleanly). Instead it seems to stop the shell process and close the connection, orphaning jupyter-notebook instead of shutting it down.

I tried writing a shell script to wrap the above command (though I would really prefer to just pass the whole thing as a ssh argument), and even tried to trap SIGINT:

#!/bin/bash

cd "$HOME/ipython"
foo() {
echo break
}
trap foo SIGINT

.direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542

but ssh remote run-jupyter.sh resulted in the same behaviour: just ^C, terminated connection, orphaned jupyter-notebook. Notably, no break output. So maybe the Ctrl-C shuts down my ssh client?

Why is this happening, and how do I fix it so Ctrl-C is correctly passed to jupyter-notebook?

(Obviously, I know I can shut down Jupyter from the webclient menu, and it will work correctly; but this is beside the point.)

The environment:

  • localhost is Mac OS Ventura 13.2.1, running GNU bash, version 5.2.15(1)-release (x86_64-apple-darwin22.1.0) under Terminal 2.13 (447)

  • remote is Ubuntu 20.04.6 LTS, running GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)"

英文:

I am trying to make an alias to easily run Jupyter on a remote machine. To this end, I concocted this command:

ssh -L 5542:localhost:5542 remote \
  'cd ipython && .direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542'

This works perfectly... except for one thing: <kbd>Ctrl-C</kbd> in the terminal does not seem to reach the jupyter-notebook process (which asks for two <kbd>Ctrl-C</kbd> presses to shut down cleanly). Instead it seems to stop the shell process and close the connection, orphaning jupyter-notebook instead of shutting it down.

I tried writing a shell script to wrap the above command (though I would really prefer to just pass the whole thing as a ssh argument), and even tried to trap SIGINT:

#!/bin/bash

cd &quot;$HOME/ipython&quot;
foo() {
  echo break
}
trap foo SIGINT

.direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542

but ssh remote run-jupyter.sh resulted in the same behaviour: just ^C, terminated connection, orphaned jupyter-notebook. Notably, no break output. So maybe the <kbd>Ctrl-C</kbd> shuts down my ssh client?

Why is this happening, and how do I fix it so <kbd>Ctrl-C</kbd> is correctly passed to jupyter-notebook?

(Obviously, I know I can shut down Jupyter from the webclient menu, and it will work correctly; but this is beside the point.)

The environment:

  • localhost is Mac OS Ventura 13.2.1, running GNU bash, version 5.2.15(1)-release (x86_64-apple-darwin22.1.0) under Terminal 2.13 (447)

  • remote is Ubuntu 20.04.6 LTS, running GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

答案1

得分: 1

我弄清楚了。当我只是登录并从生成的 shell 中调用 Jupyter 时,它可以工作;它提示差异在于进程是否有终端。这让我想到了 ssh-t 选项,即使在 ssh 执行命令时也可以强制分配终端。这是最终按预期工作的命令:

ssh -tL 5542:localhost:5542 remote \
'cd ipython && .direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542'
英文:

I figured it out. When I just log in and invoke Jupyter from the resulting shell, it works; it hints that the difference is in whether the process has a terminal or not. This led me to the -t option for ssh, forcing the allocation of the terminal even when ssh is executing a command. This is the final command that works as expected:

ssh -tL 5542:localhost:5542 remote \
  &#39;cd ipython &amp;&amp; .direnv/python-3.8.10/bin/jupyter notebook --no-browser --port 5542&#39;

答案2

得分: 0

本地的ssh进程(很可能)在按下CTRL-C时收到SIGINT信号。

您可以尝试使用stty来更改本地shell进程,使其不再解释CTRL-C,就像这样:

stty intr ctrl-x

请注意,这会将ctrl-x重新映射为中断键,所以现在ctrl-c在本地被视为普通输入。是否有效取决于远程处理原始ctrl-c的方式。

英文:

The local ssh process is (most likely) getting SIGINT when CTRL-C is pressed.

You can try changing the local shell process to not interpret the CTRL-C itself using stty like this:

stty intr ctrl-x

Note this remaps ctrl-x as the interrupt key, so now ctrl-c is treated as an ordinary input locally. Whether this works or not depends on the remote handling of the raw ctrl-c.

huangapple
  • 本文由 发表于 2023年7月20日 13:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定