博士
我希望远程执行同步启动的脚本(因此,如果准备命令失败,本地ssh命令就会失败),然后异步执行,从而终止本地ssh命令。
我在远程服务器上有一个脚本,它可以缓慢地下载一个千兆字节文件。我想使用SSH远程服务器'nohup /path/ to /script参数‘通过ssh从本地服务器启动这个脚本,但是当我知道脚本已经成功启动下载时,就会关闭SSH连接。一旦启动,SSH连接就没有任何有用的用途,在下载过程中出现系统故障,并阻止本地服务器上的执行。
我不能只执行ssh -f或ssh &操作,因为如果远程脚本没有启动,下载前的第一个命令失败,或者远程服务器无法访问,则需要命令在本地服务器上失败。
我尝试过各种nohup和screen技巧。最接近我的是:
download.sh中:初步说明#如果到目前为止有任何故障,那么本地服务器的ssh命令应该同步失败下载命令&一些更多的检查屏幕-d长下载#,我们现在可以安全地结束ssh会话了。但screen还是被杀了…
/path/to/test.sh:#!/bin/bash #取消注释这一行,以验证同步故障睡眠10 && echo >/tmp/bar & screen -d长时间下载发布于 2013-07-19 19:28:57
看看ssh子系统。我做类似的事情,但不使用脚本(例如,我使用a.out/elf编译的程序)。但我刚刚用bash脚本进行了测试,结果成功了。
来自ssh手册页:
-s May be used to request invocation of a subsystem on the remote
system. Subsystems are a feature of the SSH2 protocol which
facilitate the use of SSH as a secure transport for other appli-
cations (eg. sftp(1)). The subsystem is specified as the remote
command.在客户端上使用-s (小写)指定子系统
将另一个子系统条目添加到服务器上的sshd_config中,指向您的脚本。
# override default of no subsystems
Subsystem logger /usr/local/libexec/my-logger.sh
Subsystem sftp /usr/libexec/sftp-server您的脚本应该像任何其他具有执行权限的文件一样启动。我的例子:
#! /usr/local/bin/bash
logger "testing 1.2.3"
echo "return text from my-logger"
exec 0>&- # close stdin
exec 0<&-
exec 1>&- # close stdout
exec 1<&-
exec 2>&- # close stderr
exec 2<&-
logger "subsystem: nohup new script"
nohup /path/to/another/script/logger2.sh &
logger "subsystem: the subsystem started script is now done"
exit 0 您应该能够为成功或失败返回有用的错误文本。ssh连接(因此是stdin、stdout和stderr)一直到子系统结束为止。子系统可以继续运行,也可以不运行。
让您的另一个脚本(上面的logger2.sh)睡5-10分钟,这样您就可以使用netstat看到ssh连接消失,ssh客户机返回shell,例如ps ax显示脚本仍然在后台运行。这个脚本可能还需要关闭fd等,这只是一个快速的例子。祝好运。
https://serverfault.com/questions/524738
复制相似问题