Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Safe cross-platform command execution and capture of STDOUT & STDERR

by mattski (Initiate)
on Nov 25, 2005 at 14:51 UTC ( [id://511670]=perlquestion: print w/replies, xml ) Need Help??

mattski has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to be able to safely execute local commands on both Unix and Win32 platforms in a way that captures both the STDERR and STDOUT from the command using Perl 5.6.1. The code must have a configurable timeout too. Has anyone got any ideas about the best way to do this? I had thought about something along these lines but I can't see a way of easily getting STDERR & STDOUT with this. Any suggestions for improvement are also most welcome ...
my $err; my @output; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alert 10; my $pid = open(PIPE,"-|") or die "Error: $!"; exec @command_array or die "Error: $!"; while(<PIPE>) { push(@output,$_); } close PIPE; if(kill('TERM',$pid)) { warn "Killed PID $pid\n"; } }; alarm 0; if($@ && $@ ne "alarm\n") { $err = "Non-timeout error: $@"; push(@output,$err); } else { $err = "Timeout error: $@"; push(@output,$err); }
  • Comment on Safe cross-platform command execution and capture of STDOUT & STDERR
  • Download Code

Replies are listed 'Best First'.
Re: Safe cross-platform command execution and capture of STDOUT & STDERR
by xdg (Monsignor) on Nov 25, 2005 at 15:22 UTC

    I've had some good experiences with IPC::Run3 -- it aims for speed, simplicity and portability. Its docs have some comparisons to other alternatives, such as IPC::Open3 etc. It doesn't have a timeout, but I think your alarm code might work.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Safe cross-platform command execution and capture of STDOUT & STDERR
by Perl Mouse (Chaplain) on Nov 25, 2005 at 15:00 UTC
    What do you mean by "safely executing local commands"? Different people mean different things by that, and the answers you are seeking can very well depend on that. It seems that you can do something that is "unsafe". But what is it?

    Having said that, are you familiar with IPC::Open3? If yes, why doesn't it satisfy your needs? If no, then you should first study IPC::Open3.

    Perl --((8:>*
      Hi, Thanks for the reply, sorry I should have expanded on that. This code will be used to execute commands defined by the user but I want to do it in as safe-a-way as possible. I'll take a look at IPC::Open3. thanks, Matt
        This code will be used to execute commands defined by the user but I want to do it in as safe-a-way as possible.
        What do you mean by that? What is "safe"? Should it ask the user if the command is going to remove a file? Should it try to second-guess the user and ask confirmation if the command the user gives may not be what he really wants? Do you first want to make a backup?

        Your definition is utterly vague. As safe as possible would be to not do anything - but I doubt that is what you mean. What are you afraid of might happen? What do you want to protect yourself against?

        Perl --((8:>*
Re: Safe cross-platform command execution and capture of STDOUT & STDERR
by Anonymous Monk on Nov 25, 2005 at 14:57 UTC
    IPC::Open3?
Re: Safe cross-platform command execution and capture of STDOUT & STDERR
by secret (Beadle) on Nov 25, 2005 at 15:05 UTC

    I think that what i would do is test for the platform in a consistent way ( Sys::Hostname ) and branch the program in two cases depending on the result.

    This way you can use the windows way of redirecting STD* which is Win32::Job .

Re: Safe cross-platform command execution and capture of STDOUT & STDERR
by Moron (Curate) on Nov 25, 2005 at 16:15 UTC
    The following redirection captures both STDOUT and STDIN and works equally well in DOS as in unix sh:
    myprog.pl > myprog.log 2>&1
    (tested on both platforms)

    -M

    Free your mind

      hi, thanks for the suggestion - IPC::Run3 is perfect for what I need. Matt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://511670]
Approved by jfroebe
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found