From: Tanaka Akira Date: 2006-03-28T10:48:15+09:00 Subject: Re: IO#popen and exit status In article <20060320162018.GG5960@diehard.n-r-g.com>, Claudio Jeker writes: > I'm a bit stuck with this porblem: > I need to write data to a process and then wait for the exit code of it to > see if it was successful or not. > > Now I can IO.popen the process but there is no pclose instance method to > get the exitcode back. Additionally my workaround via Process.waitpid2 does > not work either. I think io.close does already all the cleanup. $? can be used after close. I hope this documentation helps. Index: io.c =================================================================== RCS file: /src/ruby/io.c,v retrieving revision 1.246.2.97 diff -u -p -r1.246.2.97 io.c --- io.c 14 Feb 2006 02:23:33 -0000 1.246.2.97 +++ io.c 28 Mar 2006 01:47:15 -0000 @@ -2149,6 +2149,9 @@ rb_io_close(io) * an IOError is raised if such an attempt is made. I/O * streams are automatically closed when they are claimed by the * garbage collector. + * + * If ios is opened by IO.popen, + * close sets $?. */ static VALUE @@ -3062,7 +3065,9 @@ retry: * * If a block is given, Ruby will run the command as a child connected * to Ruby with a pipe. Ruby's end of the pipe will be passed as a - * parameter to the block. In this case IO::popen returns + * parameter to the block. + * At the end of block, Ruby close the pipe and sets $?. + * In this case IO::popen returns * the value of the block. * * If a block is given with a cmd_string of ``-'', @@ -3078,6 +3083,7 @@ retry: * puts "Parent is #{Process.pid}" * IO.popen ("date") { |f| puts f.gets } * IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f}"} + * p $? * * produces: * @@ -3086,6 +3092,7 @@ retry: * Wed Apr 9 08:53:52 CDT 2003 * 26169 is here, f is * 26166 is here, f is # + * # */ static VALUE -- Tanaka Akira