Start an external application - How?

I don’t need details, just a push in the right direction.

How do you start a series of external applications from within ruby (on
windows). For example, I want to start mywebcamapp.exe and
myweatherapp.exe.

Also, is it possible to have a ruby script that will stop an external
process?

Thanks,
joe

On Aug 5, 2006, at 10:32 AM, Joe P. wrote:

I don’t need details, just a push in the right direction.

How do you start a series of external applications from within ruby
(on
windows). For example, I want to start mywebcamapp.exe and
myweatherapp.exe.

See the docs for the Kernel#system method.

Also, is it possible to have a ruby script that will stop an external
process?

I don’t know if this will work on windows, but see the docs for
Process.kill

You could try using ‘system’, eg:

system(“yourwebcamapp.exe”)

David J. wrote:

You could try using ‘system’, eg:

system(“yourwebcamapp.exe”)

system seems to want the external process to complete before continuing.
exec seems to wait for completion of the external process as well. I
need to start an external process and then go on to the next command
without waiting for the external process to complete. It would also be
nice if I could capture a handle for the external process so that I
could check status or kill it later.
BTW, I only tested operation in IRB. I’ll try both commands in a script
later.
joe

Thanks!
joe
Sard A. wrote:

This code works for me.

Joe P. wrote:

David J. wrote:

You could try using ‘system’, eg:

system(“yourwebcamapp.exe”)

system seems to want the external process to complete before continuing.
exec seems to wait for completion of the external process as well. I
need to start an external process and then go on to the next command
without waiting for the external process to complete. It would also be
nice if I could capture a handle for the external process so that I
could check status or kill it later.

This code works for me. If you start the process in a seperate thread
the rest of the Ruby script will continue to execute. To terminate it
you need some help from the operating system by using the win32ole
library http://www.ruby-doc.org/stdlib/libdoc/win32ole/rdoc/index.html

require ‘win32ole’

program = “notepad.exe”

Thread.new {system(program)}
puts "Waiting 5 seconds before terminating " + program
sleep 5

class WIN32OLE
def to_a
a = []; self.each{ |p| a<<p }; a
end
end

mgmt = WIN32OLE.connect(‘winmgmts:\\.’)
process = mgmt.InstancesOf(“win32_process”).to_a.find{ |proc|
proc.name =~ /#{program}/ }
process.Terminate

The win32ole code was copied from a Google G. thread. Some others
here might be able to talk you through it :smiley:

Hi,

From my C time, it seems like the famous fork+exec+wait. I don’t know if
it’s possible in ruby or in all platforms. May you can start a thread
and run the system there.

–Romeu

-----Mensagem original-----
De: [email protected] [mailto:[email protected]] Em nome de
Joe P.
Enviada em: quinta-feira, 10 de agosto de 2006 08:38
Para: ruby-talk ML
Assunto: Re: Start an external application - How?

David J. wrote:

You could try using ‘system’, eg:

system(“yourwebcamapp.exe”)

system seems to want the external process to complete before continuing.
exec seems to wait for completion of the external process as well. I
need to start an external process and then go on to the next command
without waiting for the external process to complete. It would also be
nice if I could capture a handle for the external process so that I
could check status or kill it later.
BTW, I only tested operation in IRB. I’ll try both commands in a script
later.
joe


Posted via http://www.ruby-forum.com/.


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.8/415 - Release Date: 9/8/2006

Joe P. wrote:

joe


Posted via http://www.ruby-forum.com/.

Anyone more knowledgable, please correct the below:
Basically, you will either want to use Threads or multiple processes.I
don’t have much experience with threads, and little more with
processes. From the pickaxe (Programming Ruby), it seems that threads
may wait for the operating system call to finish before allowing both
itself and all other threads to continue. A thread is also internal to
the currently running ruby or irb session, and as such are much more
useful executing ruby code. That being said, Threads provide alot of
neat features and such.

What you probably want is a new process.

fork do
my_program.exe
end

and then if you ever want you program to wait for the termination of
all your applications running, use:

Process.waitall

Note that forking and new processes and such are many times operating
system dependent. I have no clue as to if Windows supports this (I
think it does, it may be pipes that it has more issues with).

On 8/11/06, [email protected] [email protected] wrote:

may wait for the operating system call to finish before allowing both

and then if you ever want you program to wait for the termination of
all your applications running, use:

Process.waitall

Note that forking and new processes and such are many times operating
system dependent. I have no clue as to if Windows supports this (I

On Windows, fork in not implemented. :frowning:
For this purpose I mainly use start "" progname.exe (note the double
quotes:
if the first parameter has quotes, it’s the title of the window. That
means `start “c:\Program Files\Anything.exe” won’t start the
program…) start will start program and return, so ruby can
continue…

Jano

Joe P. wrote:

could check status or kill it later.
BTW, I only tested operation in IRB. I’ll try both commands in a script
later.
joe

Start up each process in a thread and when your at a port that you need
to do something after the processes have completed wait for the threads
to complete and continue.

On Fri, 11 Aug 2006, Jan S. wrote:

On Windows, fork in not implemented. :frowning:
For this purpose I mainly use start "" progname.exe (note the double
quotes:
if the first parameter has quotes, it’s the title of the window. That
means `start “c:\Program Files\Anything.exe” won’t start the
program…) start will start program and return, so ruby can
continue…

If it helps, I use the following bit of code when I need to
platform-independently start an external process within my test suites.
It depends on win32/process being available on Windows.

Usage is: IWATestSupport.create_process(app_to_start)

i.e. IWATestSupport.create_process(‘webrick.rb’)

module IWATestSupport
def self.create_process(args)
@fork_ok = true unless @fork_ok == false
pid = nil
begin
raise NotImplementedError unless @fork_ok
unless pid = fork
Dir.chdir args[:dir]
exec(*args[:cmd])
end
rescue NotImplementedError
@fork_ok = false
begin
require ‘rubygems’
rescue Exception
end

   begin
     require 'win32/process'
   rescue LoadError
     raise "Please install win32-process to run all tests on a Win32 

platform. ‘gem install win32-process’ or
http://rubyforge.org/projects/win32utils"
end
cwd = Dir.pwd
Dir.chdir args[:dir]
pid = Process.create(:app_name => args[:cmd].join(’ '))
Dir.chdir cwd
end
pid
end
end

Kirk H.