首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SSH进行远程同步-然后异步执行

使用SSH进行远程同步-然后异步执行
EN

Server Fault用户
提问于 2013-07-19 12:48:50
回答 1查看 3.4K关注 0票数 3

TL;

博士

我希望远程执行同步启动的脚本(因此,如果准备命令失败,本地ssh命令就会失败),然后异步执行,从而终止本地ssh命令。

详细信息

我在远程服务器上有一个脚本,它可以缓慢地下载一个千兆字节文件。我想使用SSH远程服务器'nohup /path/ to /script参数‘通过ssh从本地服务器启动这个脚本,但是当我知道脚本已经成功启动下载时,就会关闭SSH连接。一旦启动,SSH连接就没有任何有用的用途,在下载过程中出现系统故障,并阻止本地服务器上的执行。

我不能只执行ssh -fssh &操作,因为如果远程脚本没有启动,下载前的第一个命令失败,或者远程服务器无法访问,则需要命令在本地服务器上失败。

我尝试过各种nohupscreen技巧。最接近我的是:

  • 由本地服务器启动: ssh -t me@remote server 'screen -S long- -S /path/to/Dowload.sh‘或ssh -t me@remote-server 'nohup long-download /path/to/Dowload.sh’
  • 在远程服务器上启动的download.sh中:初步说明#如果到目前为止有任何故障,那么本地服务器的ssh命令应该同步失败下载命令&一些更多的检查屏幕-d长下载#,我们现在可以安全地结束ssh会话了。

screen还是被杀了…

示例再现代码

  • 在远程服务器上,在/path/to/test.sh:#!/bin/bash #取消注释这一行,以验证同步故障睡眠10 && echo >/tmp/bar & screen -d长时间下载
  • 本地: ssh -t me@remote-server 'nohup屏幕-S长-下载/path/to/test.sh‘
EN

回答 1

Server Fault用户

回答已采纳

发布于 2013-07-19 19:28:57

看看ssh子系统。我做类似的事情,但不使用脚本(例如,我使用a.out/elf编译的程序)。但我刚刚用bash脚本进行了测试,结果成功了。

来自ssh手册页:

代码语言:javascript
复制
     -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中,指向您的脚本。

代码语言:javascript
复制
    # override default of no subsystems
Subsystem       logger  /usr/local/libexec/my-logger.sh
Subsystem       sftp    /usr/libexec/sftp-server

您的脚本应该像任何其他具有执行权限的文件一样启动。我的例子:

代码语言:javascript
复制
#! /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等,这只是一个快速的例子。祝好运。

票数 2
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/524738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档