In a very simplified scenario, I have a script that looks like this:
在一个非常简单的场景中,我有这样一个脚本:
mv test _test
sleep 10
echo $1
mv _test test
and if I execute it with:
如果我用:
ssh localhost "test.sh foo"
the test file will have an underscore in the name as long as the script is running, and when the script is finished, it will send foo
back. The script SHOULD keep running, even if you terminate the ssh
command by pressing ctrl+c
or if you lose connection the the server, but it doesn't (the file is not renamed back to "test"). So, I tried the following:
只要脚本正在运行,测试文件将在名称中有一个下划线,当脚本完成时,它将发送foo返回。脚本应该继续运行,即使您通过按ctrl+c或断开连接服务器的方式来终止ssh命令,但它不会(文件没有重命名为“test”)。所以,我尝试了以下方法:
nohup ssh localhost "test.sh foo"
and it makes ssh immune to ctrl+c
but flaky connection to the server still causes trouble. After some debugging, it turns out that the script WILL actually reach the end IF THERE IS NO ECHO IN IT. And when you think about it, it makes sense - when the connection is dropped, there is no more stdout
(ssh socket) to echo to, so it will fail, silently.
而且它使ssh可以不受ctrl+c的影响,但是与服务器的连接仍然会引起麻烦。经过一些调试之后,如果脚本中没有回波,那么实际上脚本将到达末尾。当您考虑它时,它是有意义的——当连接被删除时,不再需要对stdout (ssh套接字)进行响应,因此它将无声地失败。
I can, of course, echo to a file and then get the file, but I would prefer something smarter, along the lines of test tty && echo $1
(but tty
invoked like this always returns false
). Any suggestions are greatly appreciated.
当然,我可以回显到一个文件,然后获取该文件,但是我更喜欢更智能的,类似于测试tty && echo $1的代码(但是tty这样调用总是返回false)。如有任何建议,我们将不胜感激。
3 个解决方案
#1
1
The following command does what you want:
以下命令执行所需的操作:
ssh -t user@host 'nohup ~/test.sh foo > nohup.out 2>&1 & p1=$!; tail -f ~/nohup.out & wait $p1'
... test.sh is located in the users home directory
…测试。sh位于用户主目录中
Explanation:
解释:
1.) "ssh -t user@host " ... pretty clear ... starts remote session
2.) "nohup ~/test.sh foo > nohup.out 2>&1" ... starts the test.sh script with nohup in background
3.) "p1=$!;" ... stores the child pid of the previous command in p1
4.) "tail -f ~/nohup.out &" ... tail nohup.out in background to see the output of test.sh
5.) "wait $p1" ... waits for proccess test.sh (which pid is stored in p1) to finish
1)。“ssh -t user@host”…很清楚……开始远程会话2。)“nohup ~ /测试。sh foo > nohup。2 > & 1”……开始测试。sh脚本,背景为nohup 3)“p1 = $ !”…在p1 4中存储前一个命令的子pid。“尾- f ~ / nohup。&”……尾巴nohup。在后台看到输出的测试。sh 5。)“等待美元p1”……等待过程中测试。sh (pid存储在p1中)完成
The above command works even if you interrupt it with ctrl+c.
即使使用ctrl+c中断,上面的命令仍然有效。
#2
1
you can use ...
您可以使用…
ssh -t localhost "test.sh foo"
... to force a tty allocation
…强制分配
#3
0
As st0ne suggested, tail
fails, but does not cause the script to terminate, as opposed to cat
and echo
. So, there is no need for nohup
, redirecting stdout
to a temporary file, etc. just plain and simple:
正如st0ne所建议的,tail失败,但是不会导致脚本终止,这与cat和echo相反。因此,不需要nohup,将stdout重定向到一个临时文件等简单明了:
mv test _test
sleep 10
echo $1 | tail
mv _test test
and execute it with:
并执行:
ssh localhost "test.sh foo"
#1
1
The following command does what you want:
以下命令执行所需的操作:
ssh -t user@host 'nohup ~/test.sh foo > nohup.out 2>&1 & p1=$!; tail -f ~/nohup.out & wait $p1'
... test.sh is located in the users home directory
…测试。sh位于用户主目录中
Explanation:
解释:
1.) "ssh -t user@host " ... pretty clear ... starts remote session
2.) "nohup ~/test.sh foo > nohup.out 2>&1" ... starts the test.sh script with nohup in background
3.) "p1=$!;" ... stores the child pid of the previous command in p1
4.) "tail -f ~/nohup.out &" ... tail nohup.out in background to see the output of test.sh
5.) "wait $p1" ... waits for proccess test.sh (which pid is stored in p1) to finish
1)。“ssh -t user@host”…很清楚……开始远程会话2。)“nohup ~ /测试。sh foo > nohup。2 > & 1”……开始测试。sh脚本,背景为nohup 3)“p1 = $ !”…在p1 4中存储前一个命令的子pid。“尾- f ~ / nohup。&”……尾巴nohup。在后台看到输出的测试。sh 5。)“等待美元p1”……等待过程中测试。sh (pid存储在p1中)完成
The above command works even if you interrupt it with ctrl+c.
即使使用ctrl+c中断,上面的命令仍然有效。
#2
1
you can use ...
您可以使用…
ssh -t localhost "test.sh foo"
... to force a tty allocation
…强制分配
#3
0
As st0ne suggested, tail
fails, but does not cause the script to terminate, as opposed to cat
and echo
. So, there is no need for nohup
, redirecting stdout
to a temporary file, etc. just plain and simple:
正如st0ne所建议的,tail失败,但是不会导致脚本终止,这与cat和echo相反。因此,不需要nohup,将stdout重定向到一个临时文件等简单明了:
mv test _test
sleep 10
echo $1 | tail
mv _test test
and execute it with:
并执行:
ssh localhost "test.sh foo"