终止由 bash 脚本执行的 SSH 会话 [英] Terminating SSH session executed by bash script
问题描述
我有一个可以在本地运行以远程启动服务器的脚本:
I have a script I can run locally to remotely start a server:
#!/bin/bash
ssh user@host.com <<EOF
nohup /path/to/run.sh &
EOF
echo 'done'
运行 nohup 后,它挂起.我必须按 ctrl-c 才能退出脚本.
After running nohup, it hangs. I have to hit ctrl-c to exit the script.
我尝试在此处文档的末尾添加一个显式退出,并为 ssh 使用-t"参数.两者都不起作用.如何让这个脚本立即退出?
I've tried adding an explicit exit at the end of the here doc and using "-t" argument for ssh. Neither works. How do I make this script exit immediately?
客户端是 OSX 10.6,服务器是 Ubuntu.
The client is OSX 10.6, server is Ubuntu.
推荐答案
我认为问题是当你从 ssh 进来时 nohup 不能重定向输出,它只在它认为它连接到一个终端,即使使用 -t
,我的 stdin 覆盖也会阻止这种情况发生.
I think the problem is that nohup can't redirect output when you come in from ssh, it only redirects to nohup.out when it thinks it's connected to a terminal, and I the stdin override you have will prevent that, even with -t
.
一种解决方法可能是自己重定向输出,然后 ssh 客户端可以断开连接 - 它不会等待流关闭.类似的东西:
A workaround might be to redirect the output yourself, then the ssh client can disconnect - it's not waiting for the stream to close. Something like:
nohup /path/to/run.sh > run.log &
(在从 OS X 客户端连接到 Ubuntu 服务器的简单测试中,这对我有用.)
这篇关于终止由 bash 脚本执行的 SSH 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!