简体   繁体   English

如何从远程执行的命令中获取PID?

[英]How to get PID from remote executed command?

If I do the following in Bash, then I get the PID of the remotely started mbuffer , and even though mbuffer is still running, I get the terminal back, which is what I want. 如果我在Bash中执行以下操作,那么我将获得远程启动的mbuffer的PID,即使mbuffer仍在运行,我也需要返回终端。

read -r pid < <(ssh 10.10.10.47 'nohup /opt/omni/bin/mbuffer -4 -s 128k -m 2G -v 0 -q -I 8023 >/tmp/mtest </dev/null 2>/tmp/mtest.err & echo $!')

echo $pid

Now I would like to do the same in Perl, so I try 现在我想在Perl中做同样的事情,所以我尝试

use Capture::Tiny 'capture';

my ($stdout, $stderr, $exit) = capture {
    system("read -r pid < <(ssh 10.10.10.47 'nohup /opt/omni/bin/mbuffer -4 -s 128k -m 2G -v 0 -q -I 8023 >/tmp/mtest </dev/null 2>/tmp/mtest.err & echo $!'); echo \$pid");
};

print "stdout $stdout\n";
print "stderr $stderr\n";
print "exit   $exit\n";

Here I would have expected that $stdout would have given me the PID from the last echo command, but I got nothing. 在这里,我本以为$stdout会从上一个echo命令给我PID,但是我什么也没得到。

Question

How do I get the PID of the remotely executed mbuffer in Perl, and so the Perl script isn't waiting for mbuffer to exit before continuing? 如何获取Perl中远程执行的mbuffer的PID,因此Perl脚本在继续操作之前不等待mbuffer退出?

The problem seams to be that it is not possible to execute two commands in one system() or maybe it is, but not possible to get the output from the last command. 问题似乎是,不可能在一个system()执行两个命令,或者也许不能,但是不可能从最后一个命令获得输出。

Creating a local helper script solved the problem. 创建本地帮助程序脚本解决了该问题。

#!/usr/bin/bash

# Redirection of stdin and stderr to files (preventing them from holding
# handles that connect, eventually, to the terminal).

read -r pid < <(ssh $1 "/usr/gnu/bin/nohup /opt/omni/bin/mbuffer -4 -s 128k -m 2G -v 0 -q -I 8023 >/tmp/mtest$2 </dev/null 2>/tmp/mtest.err & echo \$!")

echo $pid

and in Perl 在Perl中

my ($stdout, $stderr, $exit) = capture {
     system("/comp/mbuffer-zfs-listen.sh 10.10.10.47 11");
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM