【问题标题】:How to assign to a bash variable an ssh remote command the pid while capturing its如何在捕获 pid 时为 bash 变量分配 ssh 远程命令
【发布时间】:2016-05-05 01:01:04
【问题描述】:

简介

我的问题与this one 非常相似,只是我希望将命令的输出重定向到本地文件而不是远程文件。

提问者要求一种方法来使用类似于此的命令检索进程 ID,其中 mbuffer 命令不会导致挂起:

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

回答者回复了以下命令来解决问题

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

这确实很有帮助,但仍将文件放在远程机器上,而不是本地机器上。

我的尝试

以下是我尝试捕获 $command 输出的日志:

read -r PID &lt; &lt;(ssh $remote 'nohup $command &gt;&amp;2 &amp; echo $!' 2&gt; $log)

将 PID 正确设置为进程 ID 但不生成日志。

问题

如何在我的$command 的标准输出的本地计算机上捕获日志,同时仍将PID 分配给$command 的进程ID?

【问题讨论】:

  • 最简单的方法是将PID存储在一个文件中,然后通过scp拉取它。 expectnetcat 是避免存储远程文件的替代方法,但 IMO 不太健壮且不值得麻烦。

标签: linux bash shell ssh


【解决方案1】:

另一种方法:

{ read -r pid;
  # Do whatever you want with $pid of the process on remote machine
  cat > my_local_system_log_file
} <(ssh 10.10.10.46 "mkfifo /tmp/mtest; mbuffer -4 -v 0 -q -I 8023 &> /tmp/mtest & echo $!; cat /tmp/mtest");

基本上,第一行是 PID,后面的行是进程的日志。

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2011-03-19
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多