【问题标题】:Send tee command via ssh通过 ssh 发送 tee 命令
【发布时间】:2016-12-21 11:41:00
【问题描述】:

我从主机 1 向主机 2 发送 tee 命令:

ssh user@host2 '/path/run |& tee myFile.txt'

我使用tee,以便将二进制输出添加到myFile.txt

然后我遇到的问题是一段时间后,我想重新获得对本地主机的控制权,而无需大量打印输出。所以我做CTRL + C。这让 host2 上的进程继续运行,这是我想要的,但它会停止 tee 进程本身,因此不会填充文件。

我尝试将|& tee myFile.txt' 替换为2>&1 myFile.txt' &,但没有帮助。

如何确保文件继续在 host2 上填充,同时重新控制我在 host1 上的会话?

【问题讨论】:

    标签: linux ssh output tee


    【解决方案1】:

    如果要将结果记录在某个文件中(在nohup 中使用IO 重定向),则需要将所有管道包含在nohup 中。它不使用shell扩展,因为参数只有COMMANDARGS,所以使用sh是一个好方法:

    ssh user@host2 'nohup sh -c "/path/run |& tee myFile.txt" &'
    

    但请注意,nohup 将断开终端与命令 ant 的连接,这可能会失败。有用的是将其直接重定向到文件:

    ssh user@host2 'nohup sh -c "/path/run &> myFile.txt" &'
    

    灵感来自SO answer

    【讨论】:

      【解决方案2】:

      使用 nohup、screen 或 tmux 作为后台进程。

      【讨论】:

      • 我尝试使用ssh user@host2 'nohup /path/run |& tee myFile.txt'ssh user@host2 'nohup /path/run 2>&1 myFile.txt' 但它不起作用。当我在启动后按 ctrl+c 时,进程继续在 host2 上运行,但文件不再记录输出。
      • 'nohup /path/run >> myFile.txt'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多