[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

IO#popen and exit status

Claudio Jeker

3/20/2006 4:21:00 PM

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.

e.g.

io = IO.popen(cmd, "w")
io.write(msg)
io.close
pid, status = Process.waitpid2(0)

Raises a "Errno::ECHILD: No child processes" exception.

How do other handle this situation? Do I need to take the hard road via
pipe, fork, exec or did I miss something obvious?
--
:wq Claudio


8 Answers

Andrew Johnson

3/20/2006 4:41:00 PM

0

On Tue, 21 Mar 2006 01:20:36 +0900, Claudio Jeker
<cjeker@diehard.n-r-g.com> wrote:

[snip]
> 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.

The $? variable will hold the exit status of the last child to terminate.

andrew

--
Andrew L. Johnson http://www.s...
What have you done to the cat? It looks half-dead.
-- Schroedinger's wife

Ara.T.Howard

3/20/2006 4:59:00 PM

0

Shea Martin

3/22/2006 8:46:00 PM

0

Andrew Johnson wrote:
> On Tue, 21 Mar 2006 01:20:36 +0900, Claudio Jeker
> <cjeker@diehard.n-r-g.com> wrote:
>
> [snip]
>> 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.
>
> The $? variable will hold the exit status of the last child to terminate.
>
> andrew
>
$? does not work with popen, or open3. I am not sure why one would want
to run a process, and monitor the output, yet not care what the exit
status is? One would think that this oversite will be fixed in future
releases.

~S

Farrel Lifson

3/22/2006 8:54:00 PM

0

Actually I just solved this a few threads below. You can just run
Process.wait after you've done with the IO.popen and it should update
$? with the correct values.

On 3/22/06, Shea Martin <null@void.0> wrote:
> Andrew Johnson wrote:
> > On Tue, 21 Mar 2006 01:20:36 +0900, Claudio Jeker
> > <cjeker@diehard.n-r-g.com> wrote:
> >
> > [snip]
> >> 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.
> >
> > The $? variable will hold the exit status of the last child to terminate.
> >
> > andrew
> >
> $? does not work with popen, or open3. I am not sure why one would want
> to run a process, and monitor the output, yet not care what the exit
> status is? One would think that this oversite will be fixed in future
> releases.
>
> ~S
>
>


Shea Martin

3/22/2006 9:00:00 PM

0

Shea Martin wrote:
> Andrew Johnson wrote:
>> On Tue, 21 Mar 2006 01:20:36 +0900, Claudio Jeker
>> <cjeker@diehard.n-r-g.com> wrote:
>>
>> [snip]
>>> 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.
>>
>> The $? variable will hold the exit status of the last child to terminate.
>>
>> andrew
>>
> $? does not work with popen, or open3. I am not sure why one would want
> to run a process, and monitor the output, yet not care what the exit
> status is? One would think that this oversite will be fixed in future
> releases.
>
> ~S
ps - you can call Process.wait to update $?, but it is not reliable if
your program is multi threaded.

Shea Martin

3/22/2006 9:13:00 PM

0


> $? does not work with popen, or open3. I am not sure why one would want
> to run a process, and monitor the output, yet not care what the exit
> status is? One would think that this oversite will be fixed in future
> releases.
>
> ~S

Before I get flamed, know Process.waitpid can be used...
~S

Tanaka Akira

3/28/2006 1:48:00 AM

0

In article <20060320162018.GG5960@diehard.n-r-g.com>,
Claudio Jeker <cjeker@diehard.n-r-g.com> 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 <code>IOError</code> is raised if such an attempt is made. I/O
* streams are automatically closed when they are claimed by the
* garbage collector.
+ *
+ * If <em>ios</em> is opened by <code>IO.popen</code>,
+ * <code>close</code> sets <code>$?</code>.
*/

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 <code>IO::popen</code> returns
+ * parameter to the block.
+ * At the end of block, Ruby close the pipe and sets <code>$?</code>.
+ * In this case <code>IO::popen</code> returns
* the value of the block.
*
* If a block is given with a <i>cmd_string</i> of ``<code>-</code>'',
@@ -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 $?
*
* <em>produces:</em>
*
@@ -3086,6 +3092,7 @@ retry:
* Wed Apr 9 08:53:52 CDT 2003
* 26169 is here, f is
* 26166 is here, f is #<IO:0x401b3d44>
+ * #<Process::Status: pid=26166,exited(0)>
*/

static VALUE
--
Tanaka Akira


TD

3/19/2011 4:19:00 PM

0

On Mar 19, 11:48 am, Tom Walls <tomwa...@gmail.com> wrote:
> On 3/19/11 8:55 AM, TD wrote:
>
> > On Mar 18, 1:39 pm, tom walls<tomwa...@gmail.com>  wrote:
> >> You guys remember how my gmail account was hacked a couple weeks ago?
> >> And now you see in the news that there's been this group of hackers
> >> attacking the gmail accounts of celebrities? I'm just sayin'.
>
> > Let me get this straight, are you suggesting that you are a celebrity
> > of sorts? Well, I can buy that.
>
> > -TD
>
> When celebrity calls it makes you happy you have caller id.

So true, I mean I wouldn 't want to pick up the phone if say, Joan
Rivers is calling.

-TD