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

With the following code, I recently got a rash of failures this afternoon, all while closing the pipe file handle.

open(QSTAT, 'qstat|') or die("failed to start qstat ($!)"); while (<QSTAT>) { # Parse output from qstat } close QSTAT or die("close qstat ($!)");
(The qstat utility lists the jobs currently running on the Sun Grid Engine.) The result string $! was empty. Has anyone else seen this, or have any comments?

I have had a qstat window up and running (using watch) all day today, and it has never crashed, so I don't (much) suspect qstat.

--t. alex
Life is short: get busy!

Replies are listed 'Best First'.
Re: Why might a close operation die?
by jasonk (Parson) on Dec 02, 2003 at 22:38 UTC

    When you open a pipe to a command, the corresponding close will return false if the command exits with a non-zero exit status or if one of the system calls involved fails. When a close fails you should also examine $? to see if it contains information about the failure. The docs for close suggest this method:

    close QSTAT or die $! ? "Error closing qstat: $!" : "Exit status $? from qstat";

    We're not surrounded, we're in a target-rich environment!
Re: Why might a close operation die?
by diotalevi (Canon) on Dec 02, 2003 at 22:51 UTC

    I'm adding this just for completeness. Other people already noted by closing handles to pipes can fail, I'll note why closing handles to files can fail as well.

    Perl normally buffers output to filehandles so if there is data pending-write to the handle then the write can fail. close() flushes the buffer prior to closing the handle so it does an implicit print of whatever was left in the buffer. If the write fails during close's flush then close will return a false value. This is just another variation of the print() function failing.

Re: Why might a close operation die?
by Anonymous Monk on Dec 02, 2003 at 22:37 UTC

    You might try checking the value of $?. From the docs on open:

    Closing any piped filehandle causes the parent process to wait for the child to finish, and returns the status value in $?.
Re: Why might a close operation die?
by Anonymous Monk on Dec 02, 2003 at 22:41 UTC
    from perldoc -f close:
    If the file handle came from a piped open "close" will addi-
    tionally return false if one of the other system calls involved
    fails or if the program exits with non-zero status.  (If the
    only problem was that the program exited non-zero $! will be
    set to 0.)  Closing a pipe also waits for the process executing
    on the pipe to complete, in case you want to look at the output
    of the pipe afterwards, and implicitly puts the exit status
    value of that command into $?.
    
    Prematurely closing the read end of a pipe (i.e. before the
    process writing to it at the other end has closed it) will
    result in a SIGPIPE being delivered to the writer.  If the
    other end can't handle that, be sure to read all the data
    before closing the pipe.
    
    it's possible the app your opening is erroring out.
Re: Why might a close operation die?
by DrHyde (Prior) on Dec 03, 2003 at 09:20 UTC
    As well as the suggestions people have already made about checking $? perhaps you should see whether qstat is sensitive to whether it's running in a tty or not.
        .. perhaps you should see whether qstat is sensitive to whether it's running in a tty or not.

      Good question .. however we run this command every minute or so, and it's only very occasionally (and recently) that we've noticed this problem. Thanks for the suggestion.

      --t. alex
      Life is short: get busy!
      yes...and do all those wonderful testing things like running your command and just directing output to a file, without parsing or piping etc.