通过SSH运行远程命令但在后台运行

时间:2014-07-17 05:15:41

标签: linux ssh

我的用例如下。我必须在远程计算机上列出文件,我使用SSH来完成此操作

命令看起来像这样

/usr/bin/ssh -2 remote_user@remote_host ls -l remote_dir

这很好用,我得到ls输出

但是当我使用nohup或在后台运行时,它会出错。这个过程就退出了。例如,以下命令无法提供任何输出

nohup /usr/bin/ssh -2 remote_user@remote_host ls -l remote_dir &

在调试模式下运行时,我看到此错误。它将所有连接视为已关闭

debug2: channel 0: read<=0 rfd 4 len -1^M
debug2: channel 0: read failed^M
debug2: channel 0: close_read^M
debug2: channel 0: input open -> drain^M
debug2: channel 0: ibuf empty^M
debug2: channel 0: send eof^M
debug2: channel 0: input drain -> closed^M
debug2: tcpwinsz: 87380 for connection: 3^M
debug2: tcpwinsz: 87380 for connection: 3^M
debug2: channel 0: rcvd eof^M
debug2: channel 0: output open -> drain^M
debug2: channel 0: obuf empty^M
debug2: channel 0: close_write^M
debug2: channel 0: output drain -> closed^M
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0^M
debug2: channel 0: rcvd close^M
debug3: channel 0: will not send data after close^M
debug2: tcpwinsz: 87380 for connection: 3^M
debug2: channel 0: almost dead^M
debug2: channel 0: gc: notify user^M
debug2: channel 0: gc: user detached^M
debug2: channel 0: send close^M

如何在后台运行远程文件列表,我不想重定向标准输出和标准错误?

有人能解释一下这里发生了什么吗?

2 个答案:

答案 0 :(得分:0)

想出SSH命令必须传递一个-t参数。

SSH的手册页说明了这个

     -t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote machine,
             which can be very useful, e.g. when implementing menu services.  Multiple -t options force tty allocation, even if
             ssh has no local tty.

在使用-t -t运行SSH命令时,我能够使用nohup运行它

nohup /usr/bin/ssh -2 -t -t remote_user@remote_host ls -l remote_dir &

您必须指定-t两次以强制进行tty分配

答案 1 :(得分:0)

当我通过IBM Urband代码部署运行SSH远程命令时,也遇到了这个问题,并且通过两次提供选项-t -t可以运行该命令。

相关问题