[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Start an external application - How?

Joe Percival

8/5/2006 2:33:00 PM

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

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

10 Answers

Logan Capaldo

8/5/2006 2:46:00 PM

0


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


> Thanks,
> joe
>
> --
> Posted via http://www.ruby-....
>


David Jones

8/5/2006 2:57:00 PM

0

You could try using 'system', eg:

system("yourwebcamapp.exe")

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.


Joe Percival

8/10/2006 12:38:00 PM

0

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-....

Romeu Henrique Capparelli Fonseca

8/10/2006 1:00:00 PM

0

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-....


--
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


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


Cliff Cyphers

8/10/2006 1:11:00 PM

0

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.
> 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.

Sard Aukary

8/10/2006 7:40:00 PM

0

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/rdoc/...

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

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

ilsemanetor@gmail.com

8/11/2006 12:20:00 AM

0


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.
>
> Also, is it possible to have a ruby script that will stop an external
> process?
>
> Thanks,
> joe
>
> --
> Posted via http://www.ruby-....

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).

Jano Svitok

8/11/2006 8:07:00 AM

0

On 8/11/06, ilsemanetor@gmail.com <ilsemanetor@gmail.com> wrote:
>
> 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.
> >
> > Also, is it possible to have a ruby script that will stop an external
> > process?
> >
> > Thanks,
> > joe
> >
> > --
> > Posted via http://www.ruby-....
>
> 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

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

khaines

8/11/2006 3:05:00 PM

0

Joe Percival

8/13/2006 1:26:00 PM

0

Thanks!
joe
Sard Aukary wrote:

> This code works for me.

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