bash 当我中断 ssh 本身时,如何让 ssh 杀死远程进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/331642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make ssh to kill remote process when I interrupt ssh itself?
提问by tkokoszka
In a bash script I execute a command on a remote machine through ssh. If user breaks the script by pressing Ctrl+C it only stops the script - not even ssh client. Moreover even if I kill ssh client the remote command is still running...
在 bash 脚本中,我通过 ssh 在远程机器上执行命令。如果用户通过按 Ctrl+C 中断脚本,它只会停止脚本 - 甚至不会停止 ssh 客户端。此外,即使我杀死了 ssh 客户端,远程命令仍在运行......
How can make bash to kill local ssh client and remote command invocation on Crtl+c?
如何让 bash 在 Crtl+c 上杀死本地 ssh 客户端和远程命令调用?
A simple script:
一个简单的脚本:
#/bin/bash
ssh -n -x root@db-host 'mysqldump db' -r file.sql
采纳答案by tkokoszka
Eventual I found a solution like that:
最终我找到了这样的解决方案:
#/bin/bash
ssh -t -x root@db-host 'mysqldump db' -r file.sql
So - I use '-t' instead of '-n'. Removing '-n', or using different user than root does not help.
所以 - 我使用'-t'而不是'-n'。删除“-n”或使用与 root 不同的用户都无济于事。
回答by Peter Cordes
When your ssh session ends, your shell will get a SIGHUP. (hang-up signal). You need to make sure it sends that on to all processes started from it. For bash, try shopt -s huponexit; your_command. That may not work, because the man page says huponexit only works for interactive shells.
当您的 ssh 会话结束时,您的 shell 将收到一个 SIGHUP。(挂断信号)。您需要确保它将其发送到所有从它启动的进程。对于 bash,请尝试shopt -s huponexit; your_command. 这可能不起作用,因为手册页说 huponexit 仅适用于交互式 shell。
I remember running into this with users running jobs on my cluster, and whether they had to use nohup or not (to get the opposite behaviour of what you want) but I can't find anything in the bash man page about whether child processes ignore SIGHUP by default. Hopefully huponexit will do the trick. (You could put that shopt in your .bashrc, instead of on the command line, I think.)
我记得用户在我的集群上运行作业时遇到过这种情况,以及他们是否必须使用 nohup(以获得与您想要的相反的行为)但我在 bash 手册页中找不到任何关于子进程是否忽略的内容默认情况下SIGHUP。希望 huponexit 可以解决问题。(你可以把那个 shopt 放在你的 .bashrc 中,而不是在命令行上,我想。)
Your ssh -tshould work, though, since when the connection closes, reads from the terminal will get EOF or an error, and that makes most programs exit.
ssh -t但是,您应该可以正常工作,因为当连接关闭时,从终端读取将获得 EOF 或错误,这会使大多数程序退出。
回答by converter42
Do you know what the options you're passing to ssh do? I'm guessing not. The -n option redirects input from /dev/null, so the process you're running on the remote host probably isn't seeing SIGINT from Ctrl-C.
你知道你传递给 ssh 的选项是做什么的吗?我猜不是。-n 选项重定向来自 /dev/null 的输入,因此您在远程主机上运行的进程可能看不到来自 Ctrl-C 的 SIGINT。
Now, let's talk about how bad an idea it is to allow remote root logins:
现在,让我们谈谈允许远程 root 登录的想法是多么糟糕:
It's a really, really bad idea. Have a look at HOWTO: set up ssh keysfor some suggestions how to securely manage remote process execution over ssh. If you need to run something with privileges remotely you'll probably want a solution that involves a ssh public key with embedded command and a script that runs as root courtesy of sudo.
这真是一个非常糟糕的主意。看看HOWTO: set up ssh keys以获得一些关于如何安全地管理通过 ssh 的远程进程执行的建议。如果您需要远程运行具有特权的某些东西,您可能需要一个解决方案,该解决方案涉及带有嵌入式命令的 ssh 公钥和由 sudo 提供的以 root 身份运行的脚本。
回答by Johannes Schaub - litb
trap "some_command" SIGINT
will execute some_commandlocally when you press Ctrl+C . help trapwill tell you about its other options.
some_command当您按 Ctrl+C 时将在本地执行。help trap会告诉你它的其他选项。
Regarding the sshissue, i don't know much about ssh. Maybe you can make it call ssh -n -x root@db-host 'killall mysqldump'instead of some_commandto kill the remote command?
关于这个ssh问题,我对ssh了解不多。也许你可以让它调用ssh -n -x root@db-host 'killall mysqldump'而不是some_command杀死远程命令?

