击:使用SSH开始一个长期运行的远程命令并收集其PID [英] Bash: Using SSH to start a long-running remote command and collect its PID

查看:169
本文介绍了击:使用SSH开始一个长期运行的远程命令并收集其PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做的话,我不得不preSS CTRL-C 之后或壳行为怪异。左/右箭头键例如不正确地移动和文本搞砸了。

 #阅读-r PID< ≤(SSH 10.10.10.46的睡眠50安培;!回声$');回声$ PID
2135
#由信号2杀害。
^ C

我需要这个剧本,所以我想知道为什么需要 CTRL-C ,是有可能解决它?

更新

它看起来像它会打开一个额外的Bash shell,这是一个需要退出的人。

我在实际有趣的命令是

 阅读-r PID< ≤(SSH 10.10.10.46mbuffer -4 -v 0 -q -I 8023>的/ tmp / m试验和放大器;!回声$);回声$ PID


解决方案

试试这个:

 阅读-r PID \\
  < ≤(SSH 10.10.10.46'的nohup mbuffer>的/ tmp / m试验<的/ dev / null的2 - ; /tmp/mtest.err和放大器;!回声$')

三个重要的变化:


  • 的nohup 的使用(你也可以得到与内置的不认在bash类似的效果)

  • 标准输入和标准错误的文件($ P $持有连接,最终到终端手柄pventing他们)的重定向。

  • 使用单引号远程命令(用双引号,扩张前 SSH 开始发生,因此 $!你得到的是最新开始的本地的后台进程的PID)。

When I do the following, then I have to press CTRL-c afterwards or the shell acts weird. Left/right arrows keys e.g. doesn't move correctly and the text is messed up.

# read -r pid < <(ssh 10.10.10.46 'sleep 50 & echo $!') ; echo $pid
2135
# Killed by signal 2.
^C
#

I need this for a script, so I'd like to know why CTRL-c is needed and is it possible to work around it?

Update

It looks like it opens an extra Bash shell, and that is the one that needs to be exited.

The command I am actually interesting in is

read -r pid < <(ssh 10.10.10.46 "mbuffer -4 -v 0 -q -I 8023 > /tmp/mtest & echo $!"); echo $pid

解决方案

Try this instead:

read -r pid \
  < <(ssh 10.10.10.46 'nohup mbuffer >/tmp/mtest </dev/null 2>/tmp/mtest.err & echo $!')

Three important changes:

  • Use of nohup (you could also get a similar effect with the bash built-in disown)
  • Redirection of stdin and stderr to files (preventing them from holding handles that connect, eventually, to your terminal).
  • Use of single quotes for the remote command (with double-quotes, expansions happen before ssh is started, so the $! you get is the PID of the most recently started local background process).

这篇关于击:使用SSH开始一个长期运行的远程命令并收集其PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆