Hi all,
Any ideas for the question below? I know how to do this in theory - make the
''inherit'' flag true, and set the ''stdout''
and ''stderr'' startf_flags hash options to something in the
startup_info hash, but I wasn''t sure how to do this in practice.
It would be nice if the answer could be something like this:
require ''win32/process''
app_name = ''c:\ruby\bin\ruby "'' + Dir.pwd +
''/test.rb"''
my_out = $stdout.clone
my_err = $stderr.clone
my_out.reopen(''NUL'')
my_err.reopen(''NUL'')
Process.create(
:app_name => app_name,
:inherit => true,
:creation_flags => Process::DETACHED_PROCESS,
:startup_info => {
:startf_flags => Process::STARTF_USESTDHANDLES,
:stdout => my_out,
:stderr => my_err
}
)
I think it would be neat if you could pass IO objects (or perhaps a fileno) to
:stdout, :stderr, and :stdin directly. Any ideas on how to do that?
Thanks,
Dan
----- Forwarded Message ----
From: Kirill Miazine <km at krot.org>
To: Daniel J. Berger <djberg96 at yahoo.com>
Sent: Monday, November 20, 2006 12:35:40 PM
Subject: re. win32-process
Hello Daniel,-
I''m one of those poor people who sit behind a corporate firewall that
for some reason blocks access to useful ports. But I''m lucky enought to
have access to port 22, so I can fire up ssh and set up port forwarding.
I used to do so with PuTTY and it worked nice. But having that terminal
window hanging around is annoying, so I tried to find a better workaround.
I installed Ruby and win32-process. I''m new to Ruby. I''m also
new to
Windows, so I''ve no idea about how process managemenent,
stdin/stdout/stderr etc happen in this environment (and where is the
documentation?!?). (I''ve been programming Perl on *nix for several
years.)
I find out that I use the flag Process::DETACHED_PROCESS to avoid the
process window. But how do I suppress command output? I want something
like "2>&1 > /dev/null".
I saw stdin, stdout and stderr options in startup_info, but I wasn''t
able to find a solution to the
======================================require ''win32/process''
class <<$stdout
def write(stuff)
return
end
end
class <<$stderr
def write(stuff)
return
end
end
cmd = ''c:\Program Files\plink.exe -batch -N -load proxy''
p = Process.create(:app_name => cmd,
:creation_flags => Process::DETACHED_PROCESS)
Process.waitpid p.process_i
======================================
(PLink is a command-line ssh client, very handy.)
I''m grateful for any advice!
Thanks in advance!
- Kirill
____________________________________________________________________________________
Sponsored Link
Online or Campus degree Associate''s, Bachelor''s, or
Master''s
in less than one year.www.findtherightschool.com
> -----Original Message----- > From: win32utils-devel-bounces at rubyforge.org > [mailto:win32utils-devel-bounces at rubyforge.org] On Behalf Of > Daniel Berger > Sent: den 21 november 2006 05:55 > To: win32utils-devel at rubyforge.org > Subject: [Win32utils-devel] Fw: re. win32-process > > Hi all, > > Any ideas for the question below? I know how to do this in > theory - make the ''inherit'' flag true, and set the ''stdout'' > and ''stderr'' startf_flags hash options to something in the > startup_info hash, but I wasn''t sure how to do this in practice. > > It would be nice if the answer could be something like this: > > require ''win32/process'' > > app_name = ''c:\ruby\bin\ruby "'' + Dir.pwd + ''/test.rb"'' > my_out = $stdout.clone > my_err = $stderr.clone > my_out.reopen(''NUL'') > my_err.reopen(''NUL'') > > Process.create( > :app_name => app_name, > :inherit => true, > :creation_flags => Process::DETACHED_PROCESS, > :startup_info => { > :startf_flags => Process::STARTF_USESTDHANDLES, > :stdout => my_out, > :stderr => my_err > } > ) > > I think it would be neat if you could pass IO objects (or > perhaps a fileno) to :stdout, :stderr, and :stdin directly. > Any ideas on how to do that?If you implement natively using VC, and have access to either the underlying FILE* or file descriptor, take a look at: _fileno _get_osfhandle If you are doing it in pure Ruby, I don''t know. As for the original question, perhaps something like using Process.create to launch cmd.exe with the args: ''/c "c:/Program Files/plink.exe -batch -N -load proxy > 2>&1 > NUL"'' could work (haven''t tried it). / Johan
Hi, 2006/11/21, Daniel Berger <djberg96 at yahoo.com>:> > Hi all, > > Any ideas for the question below? I know how to do this in theory - make > the ''inherit'' flag true, and set the ''stdout'' and ''stderr'' startf_flags hash > options to something in the startup_info hash, but I wasn''t sure how to do > this in practice. > > It would be nice if the answer could be something like this: > > require ''win32/process'' > > app_name = ''c:\ruby\bin\ruby "'' + Dir.pwd + ''/test.rb"'' > my_out = $stdout.clone > my_err = $stderr.clone > my_out.reopen(''NUL'') > my_err.reopen(''NUL'') > > Process.create( > :app_name => app_name, > :inherit => true, > :creation_flags => Process::DETACHED_PROCESS, > :startup_info => { > :startf_flags => Process::STARTF_USESTDHANDLES, > :stdout => my_out, > :stderr => my_err > } > ) > > I think it would be neat if you could pass IO objects (or perhaps a > fileno) to :stdout, :stderr, and :stdin directly. Any ideas on how to do > that? > > Thanks, > > DanUse _get_osfhandle function like this: :stdout => _get_osfhandle(my_out.to_i), :stderr => _get_osfhandle(my_err.to_i), Regards, Park Heesob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20061121/fb330ea0/attachment.html
Heesob Park wrote:> Hi, > > 2006/11/21, Daniel Berger <djberg96 at yahoo.com > <mailto:djberg96 at yahoo.com>>: > > Hi all, > > Any ideas for the question below? I know how to do this in theory > - make the ''inherit'' flag true, and set the ''stdout'' and ''stderr'' > startf_flags hash options to something in the startup_info hash, > but I wasn''t sure how to do this in practice. > > It would be nice if the answer could be something like this: > > require ''win32/process'' > > app_name = ''c:\ruby\bin\ruby "'' + Dir.pwd + ''/test.rb"'' > my_out = $stdout.clone > my_err = $stderr.clone > my_out.reopen(''NUL'') > my_err.reopen(''NUL'') > > Process.create( > :app_name => app_name, > :inherit => true, > :creation_flags => Process::DETACHED_PROCESS, > :startup_info => { > :startf_flags => Process::STARTF_USESTDHANDLES, > :stdout => my_out, > :stderr => my_err > } > ) > > I think it would be neat if you could pass IO objects (or perhaps > a fileno) to :stdout, :stderr, and :stdin directly. Any ideas on > how to do that? > > Thanks, > > Dan > > > Use _get_osfhandle function like this: > > :stdout => _get_osfhandle(my_out.to_i), > :stderr => _get_osfhandle(my_err.to_i), > > Regards, > > Park HeesobThanks, I couldn''t remember the name of the function last night - too tired. ;) I''ve added get_osfhandle to the Windows::Handle module (in windows-pr) in CVS. Then I added this to the Process.create method: # Automatically handle stdin, stdout and stderr as either IO objects # or file descriptors. This won''t work for StringIO, however. [''stdin'', ''stdout'', ''stderr''].each{ |io| if si_hash[io] if si_hash[io].respond_to?(:fileno) handle = get_osfhandle(si_hash[io].fileno) else handle = get_osfhandle(si_hash[io]) end if handle == INVALID_HANDLE_VALUE raise ProcessError, get_last_error end si_hash[io] = handle end } That seems to work fairly well. The only quirk I noticed is that it doesn''t seem to honor the append flag for the file descriptor: # test.rb 5.times{ |i| puts "I: #{i}" } # proc_test.rb require ''win32/process'' app = ''c:\ruby\bin\ruby "'' + Dir.pwd + ''/test.rb"'' fh = File.open(''test.txt'', ''a'') Process.create( :app_name => app, :inherit => true, :creation_flags => Process::DETACHED_PROCESS, :startup_info => { :startf_flags => Process::STARTF_USESTDHANDLES, :stdout => fh } ) fh.close Instead of appending to the test.txt file, it appears to simply overwrite it each time. I can''t remember if we discussed this in the past or not. If there''s no way to deal with it, then it''s not a huge deal - I just need to document it. What do you think? Regards, Dan PS - Any way we could support StringIO as well?