Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Is it possible to detect "no file" error in open2 ?

by bdimych (Monk)
on Dec 07, 2007 at 16:13 UTC ( [id://655683]=perlquestion: print w/replies, xml ) Need Help??

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

> > > cat 1.pl use strict; use IPC::Open2; my $cmd = '/no/such/command'; open2(my ($out, $in), $cmd) || die("I thought script should die here: +$!"); sleep 1; open(my $pipe, "$cmd|") || die("But it dies only here: $!"); > > > perl 1.pl open2: exec of /no/such/command failed at 1.pl line 4 But it dies only here: No such file or directory at 1.pl line 6. > > >
How to catch such error and what is the first message?

Replies are listed 'Best First'.
Re: Is it possible to detect "no file" error in open2 ?
by ikegami (Patriarch) on Dec 07, 2007 at 16:47 UTC

    open2 successfully created the child (fork), so it returned the child's PID. But the child process was unable to execute the desired command (exec), so it died, setting the child's error code to something non-zero.

    To check if the child is still running, use
    kill(0, $pid) or
    waitpid($pid, WNOHANG) (less portable?) or
    eof($out) (good indicator, but not always correct).

    waitpid returns a number which includes the child's error code. See $? for details.

    You should be calling waitpid when using open2/open3 even if you don't care about the error code, or else you'll leave zombie processes behind.

Re: Is it possible to detect "no file" error in open2 ?
by zentara (Archbishop) on Dec 07, 2007 at 18:04 UTC
    This dies with a message from the forked pid. Although the die message on the line gets lost. I can't seem to figure out why your script dosn't work? I did find if you put a print statement BEFORE your next open statement, your script will die at the right line. Without the print, it stays alive and tries the next open. ????
    #!/usr/bin/perl use warnings; use strict; use IPC::Open2; my $cmd = '/no/such/command'; my($out,$in); open2($out, $in, $cmd) || die("I thought script should die here: $!"); print $in "the_string\n" || die ("print die here no fork: $!"); open(my $pipe, "$cmd|") || die("But it dies only here: $!");
    It gives the error:

    open2: exec of /no/such/command failed at ./655683.pl line 10


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Is it possible to detect "no file" error in open2 ?
by Anonymous Monk on Dec 07, 2007 at 20:46 UTC
    From the documentation:

    open2() returns the process ID of the child process. It doesn't return on failure: it just raises an exception matching "/^open2:/".However, "exec" failures in the child are not detected. You'll have to trap SIGPIPE yourself.

Re: Is it possible to detect "no file" error in open2 ?
by bdimych (Monk) on Dec 08, 2007 at 02:36 UTC
  • still not solved with open2/open3
  • IPC::Run and IPC::Run3 seems better. Useful comparision
  • ikegami, Anonymous Monk, couldn't you provide examples

    ---
    All my old tests work well with the following code, so I've fixed on it

    use IPC::Run3; ... eval {run3 [ 'cmd', 'without subshell', 'and with any complex params 1<>2()[]{*}' ], 'and/now/I/can', 'redirect/child/streams', 'from/to/my/files' }; retcode_full_test_copypasted_from_perldoc_f_system; if ($@) { die("run3 failed but retcode_full_test passed: $@") } ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found