Forum: Ruby Start an external application - How?

49c4935f50f26608ba704f29da8e93b4?d=identicon&s=25 Joe Percival (joep)
on 2006-08-05 16:32
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-08-05 16:48
(Received via mailing list)
On Aug 5, 2006, at 10:32 AM, Joe Percival 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
53dec0ba39eb2030bc84fea62e1471b2?d=identicon&s=25 David Jones (Guest)
on 2006-08-05 17:06
(Received via mailing list)
You could try using 'system', eg:

system("yourwebcamapp.exe")
49c4935f50f26608ba704f29da8e93b4?d=identicon&s=25 Joe Percival (joep)
on 2006-08-10 14:37
David Jones 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
A80bfa5dddb90d68133a14c7803e1e43?d=identicon&s=25 Sard Aukary (sard)
on 2006-08-10 21:40
Joe Percival wrote:
> David Jones 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/rdo...

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 Groups thread.  Some others
here might be able to talk you through it :D
49c4935f50f26608ba704f29da8e93b4?d=identicon&s=25 Joe Percival (joep)
on 2006-08-13 15:25
Thanks!
joe
Sard Aukary wrote:

> This code works for me.
5f671fccb9d0fea73357163f2685838b?d=identicon&s=25 Romeu Henrique Capparelli Fonseca (Guest)
on 2006-08-15 14:13
(Received via mailing list)
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: list-bounce@example.com [mailto:list-bounce@example.com] Em nome de
Joe Percival
Enviada em: quinta-feira, 10 de agosto de 2006 08:38
Para: ruby-talk ML
Assunto: Re: Start an external application - How?

David Jones 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
B2156f905dd13ec3f45123fbe0dc0f8f?d=identicon&s=25 Cliff Cyphers (Guest)
on 2006-08-15 14:13
(Received via mailing list)
Joe Percival 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.
C2d61fa11869bc69dc7eeffbd4f5dbae?d=identicon&s=25 Michael Ilseman (avathartri)
on 2006-08-15 14:15
(Received via mailing list)
Joe Percival 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).
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2006-08-15 14:16
(Received via mailing list)
On 8/11/06, ilsemanetor@gmail.com <ilsemanetor@gmail.com> 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. :(
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
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 unknown (Guest)
on 2006-08-15 14:16
(Received via mailing list)
On Fri, 11 Aug 2006, Jan Svitok wrote:

> On Windows, fork in not implemented. :(
> 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 Haines
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.