[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

redirecting STDOUT

Joe Van Dyk

6/27/2005 5:38:00 PM

Why doesn't this work?

(#executable is a function that returns a string that contains a test
application that spits out some data)

def start
pid = fork
if pid.nil? # In child
log = File.open(executable + ".log", "w")
STDOUT.reopen(log)
STDERR.reopen(log)
exec executable # Start program
exit
else # In parent
@pid = pid # Record new process id
Process.detach @pid # If the process dies, let it die
monitor # Start monitoring the process' status
end
end


Well, it works for the most part. The log file gets created, but
nothing gets written to it. Am I not redirecting output correctly?

Thanks,
Joe

Thanks,
Joe


7 Answers

Gennady

6/27/2005 6:23:00 PM

0

Joe Van Dyk wrote:
> Why doesn't this work?
>
> (#executable is a function that returns a string that contains a test
> application that spits out some data)
>
> def start
> pid = fork
> if pid.nil? # In child
> log = File.open(executable + ".log", "w")
> STDOUT.reopen(log)
> STDERR.reopen(log)
> exec executable # Start program
> exit
> else # In parent
> @pid = pid # Record new process id
> Process.detach @pid # If the process dies, let it die
> monitor # Start monitoring the process' status
> end
> end
>
>
> Well, it works for the most part. The log file gets created, but
> nothing gets written to it. Am I not redirecting output correctly?
>
> Thanks,
> Joe
>
> Thanks,
> Joe
>

Are you sure your "executable" is producing anything when launched the
way you do it? You may try some STDERR.puts before "exec executable" to
know for sure that something must be there. Also, I would do log.close
before exec as the file descriptor is cloned anyway for stdout and stderr.

Gennady.


Gary Wright

6/27/2005 7:48:00 PM

0


On Jun 27, 2005, at 1:37 PM, Joe Van Dyk wrote:
> Why doesn't this work?

Disclaimer: I'm pretty good with Unix System stuff
but still learning on the Ruby so...

> (#executable is a function that returns a string that contains a test
> application that spits out some data)
>
> def start
> pid = fork
> if pid.nil? # In child
> log = File.open(executable + ".log", "w")
> STDOUT.reopen(log)
> STDERR.reopen(log)
> exec executable # Start program
> exit
> else # In parent
> @pid = pid # Record new process id
> Process.detach @pid # If the process dies, let it die
> monitor # Start monitoring the process' status
> end
> end

This worked for me (Mac OS X 10.4, Ruby 1.8.2). Check the permissions
on the log file. If was created in earlier tests as a read-only file
maybe
that is why the program fails now.

If you have something like ktrace or truss on your system, trace
the ruby program and then look at the dump. You can see exactly
what system calls are being made (open/read/write/etc) and also
what errors, if any, are being returned.

Try using some standard program such as /bin/date instead of executable
just to make sure it isn't a problem with your test program. You'll
probably want a different path for the log file in that case.

If 'monitor' is going to watch the child process then just incorporate
calls to Process.wait(@pid) as part of 'monitor' instead of
Process.detach. If you have both Process.detach and monitor
periodically
calling Process.wait, you'll never be sure which thread is going to
catch the exiting child.



Gary Wright



Joe Van Dyk

6/27/2005 8:53:00 PM

0

Nevermind, it does work!

My test application was the following Ruby application:

#!/bin/env ruby
while true
puts "pid: #{Process.pid}, time: #{Time.now}"
sleep 0.2
puts
end

Does #puts not write to STDOUT by default?


On 6/27/05, gwtmp01@mac.com <gwtmp01@mac.com> wrote:
>
> On Jun 27, 2005, at 1:37 PM, Joe Van Dyk wrote:
> > Why doesn't this work?
>
> Disclaimer: I'm pretty good with Unix System stuff
> but still learning on the Ruby so...
>
> > (#executable is a function that returns a string that contains a test
> > application that spits out some data)
> >
> > def start
> > pid = fork
> > if pid.nil? # In child
> > log = File.open(executable + ".log", "w")
> > STDOUT.reopen(log)
> > STDERR.reopen(log)
> > exec executable # Start program
> > exit
> > else # In parent
> > @pid = pid # Record new process id
> > Process.detach @pid # If the process dies, let it die
> > monitor # Start monitoring the process' status
> > end
> > end
>
> This worked for me (Mac OS X 10.4, Ruby 1.8.2). Check the permissions
> on the log file. If was created in earlier tests as a read-only file
> maybe
> that is why the program fails now.
>
> If you have something like ktrace or truss on your system, trace
> the ruby program and then look at the dump. You can see exactly
> what system calls are being made (open/read/write/etc) and also
> what errors, if any, are being returned.
>
> Try using some standard program such as /bin/date instead of executable
> just to make sure it isn't a problem with your test program. You'll
> probably want a different path for the log file in that case.
>
> If 'monitor' is going to watch the child process then just incorporate
> calls to Process.wait(@pid) as part of 'monitor' instead of
> Process.detach. If you have both Process.detach and monitor
> periodically
> calling Process.wait, you'll never be sure which thread is going to
> catch the exiting child.
>
>
>
> Gary Wright
>
>
>


Gary Wright

6/27/2005 9:21:00 PM

0


On Jun 27, 2005, at 4:52 PM, Joe Van Dyk wrote:
> Nevermind, it does work!
>
> My test application was the following Ruby application:
>
> #!/bin/env ruby
> while true
> puts "pid: #{Process.pid}, time: #{Time.now}"
> sleep 0.2
> puts
> end
>
> Does #puts not write to STDOUT by default?

Buffering. When you run this program with output
associated with the terminal, STDOUT is line buffered
so you see each line right after puts executes.

When you redirect STDOUT to a file, the IO is not
line buffered but instead buffered into blocks.
When I tested your program, I didn't see any output
in the file until puts had generated 4k worth of
data at which point it was flushed to the file
system.

You'll have to put in explicit calls to flush if
you expect to see the output in a different way or
change STDOUT to be line buffered.


Gary Wright



Ara.T.Howard

6/27/2005 9:43:00 PM

0

PaxPerPoten

7/26/2011 10:28:00 PM

0

On 7/26/2011 6:01 AM, Franklin Hummel wrote:
>
> I've been reading about that in other posts as well: that going to
> democrathallofshame.com will put a computer virus or two on your
> computer.
>
> And, of course, it will also dump all type of spyware on it as well.
> Supposedly the spyware is how Bob Milby Jr. can afford to pay for that
> ultraconservative K00K website of his.
>
> I've even heard he's put up a photo of the white-supremist, far-right-
> wing, murdering terrorist Anders Breivik, with a large caption of
> "HERO!" under it at his site!
>
> After all, Patriot Games and Breivik are two peas in a pod
> politically!

I believe Mr Breivik was killing Marxist and the people who
indoctrinated them. You surely would have been one of his targets.


--
It is hardly too strong to say that the Constitution was made to guard
the people against the dangers of good intentions. There are men in all
ages who mean to govern well, but *They mean to govern*. They promise to
be good masters, *but they mean to be masters*. Daniel Webster

Fwanklin Hummel - 18 Linwood St, Unit 3, Boston, MA 02119 (617) 541-3834

7/27/2011 1:58:00 AM

0

The DemocRAT Hall Of Shame http://www.democrathallof... asks
"Why do you always LIE?"


On Tue, 9 Mar 2010 05:22:52 -0800 (PST), "Tom Sr."
<tomswiftsenior@gmail.com> wrote:
>On Mar 8, 10:42?pm, "carl" <cstandi...@bak.rr.com> wrote:
>> retro..that is the most stupid idiotic statement I heard.
>> Barney Frank is a freaken pedophile..you nimrod.
>Provide at least two citations from well-known and generally-accepted
>news sources for your *claim* -- or shut the fuck up, Carl, you
>dumbass.

Q. Young boys and young girls with each other or with older folks?
A. "With the older folks. [...] And one person that I'm not afraid to
talk about because his, because Larry King always said him and this
guy were on the opposite ends of the field because this guy was a
Democrat and Larry King was a Republican. That's a known fact and
stuff. And this guy, every time I see him on TV and stuff, my wife
knows my hatred for him. Because every time I see him and stuff it
disgusts me because it's --his name is Barney Frank."

Q. Did you have relationships with him?
A. "Yes."

Q. Where?
A. "In Washington, D. C. And also I was sent to a house, I believe it
was in Massachusetts in Boston where I believe it was his house
because there's pictures on the wall that, with him and with different
people and stuff. And that he had met I guess. But it was in his
basement."
IN THE UNITED STATES DISTRICT COURT FOR THE DISTRICT OF NEBRASKA PAUL
A. BONACCI, 4:CV91-33037 Plaintiff,
vs.
LAWRENICE E. KING
MEMORANDUM OF DECISION
Transcript: http://www.ctrl.org/silence...

"Tom Sr." - Boston.
Bawney Fwank: Boston.

"Tom Sr." - A Known Faggot.
Bawney Fwank: A Known Faggot.

"Tom Sr." - DEFENDING Bawney Fwank...

What an amazing coincidence!

Posted from:
The DemocRATs Hall of Shame!
http://www.democrathallof...