1

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.

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

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.

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?

3
  • 2
    I would advise not using double quotes for your system command, but to instead use single quotes with an alternative delimiter such as q{}. You escaped your \$pid, but you failed to escape $!. It's therefore possible that an error is being reported but not displayed to you. Commented Aug 3, 2014 at 2:48
  • Very nice spotted. I can't make q{} work inside system(). It is ignored it seams. Commented Aug 3, 2014 at 14:30
  • 1
    How could someone dislike this question. +1 ;) Commented Aug 3, 2014 at 16:47

1 Answer 1

2

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.

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

my ($stdout, $stderr, $exit) = capture { system("/comp/mbuffer-zfs-listen.sh 10.10.10.47 11"); }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Anyhow your solution is the better one as system() uses /bin/sh and not /bin/bash that some of your command features like process substitution may not work. Changing the value of env. variable SHELL to /bin/sh doesn't help as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.