Launch external program

Hi all,

   I'm running external programs like this:

      status = `rdesktop .... &`

   But my ruby script doesn't work until the external program finished.
How can I run an external program asynchronously?

Thanks.

···

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

system("command")

You cannot save the output, though.

···

On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <andrey.demidov@gmail.com>wrote:

Hi all,

  I'm running external programs like this:

     status = `rdesktop .... &`

  But my ruby script doesn't work until the external program finished.
How can I run an external program asynchronously?

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

Hi,

···

Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:

On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <andrey.demidov@gmail.com>wrote:
> I'm running external programs like this:
>
> status = `rdesktop .... &`
>
> But my ruby script doesn't work until the external program finished.
> How can I run an external program asynchronously?
>
system("command")

You cannot save the output, though.

Yes, you can.

  output = ""
  t = Thread.new { output << `somecmd` }
  ...
  t.alive? or puts "The full output is:", output

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

  I'm running external programs like this:

     status = `rdesktop .... &`

  But my ruby script doesn't work until the external program finished.
How can I run an external program asynchronously?

system("command")

Christopher, system does not execute commands asynchronously.

You cannot save the output, though.

Yes, you can.

Obama? :slight_smile:

  output = ""
  t = Thread.new { output << `somecmd` }
  ...
  t.alive? or puts "The full output is:", output

You can do this as well:

t = Thread.new { `somecmd` }
...
output = t.value

It seems Thread#value is rarely used but it can be of very useful.

Kind regards

  robert

···

On 17.06.2009 16:02, Bertram Scharpf wrote:

Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:

On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <andrey.demidov@gmail.com>wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

···

On 17.06.2009 16:02, Bertram Scharpf wrote:

  output = ""
  t = Thread.new { output << `somecmd` }
  ...
  t.alive? or puts "The full output is:", output

You can do this as well:

t = Thread.new { `somecmd` }
...
output = t.value

Thread#value blocks while the thread is running, though.

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Hi,

···

Am Donnerstag, 18. Jun 2009, 05:45:08 +0900 schrieb Robert Klemme:

On 17.06.2009 16:02, Bertram Scharpf wrote:

  output = ""
  t = Thread.new { output << `somecmd` }
  ...
  t.alive? or puts "The full output is:", output

You can do this as well:

t = Thread.new { `somecmd` }
...
output = t.value

Gosh, that's the programming style I was looking for.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Joel VanderWerf wrote:

Robert Klemme wrote:

output = t.value

Thread#value blocks while the thread is running, though.

Ok but how would you in Windows start a seperate program like a vendor
software that runs seperate from the ruby program? If I try
`run_me.exe` or
system("run_me.exe") the ruby program just sits waiting for the other to
finsh?

···

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

Well, yes. But what is your point? If you want the result you need
to wait anyway. If not you can still use your pattern:

t.alive? or puts "The full output is:", t.value

Kind regards

robert

···

2009/6/17 Joel VanderWerf <vjoel@path.berkeley.edu>:

Robert Klemme wrote:

On 17.06.2009 16:02, Bertram Scharpf wrote:

output = ""
t = Thread.new { output << `somecmd` }
...
t.alive? or puts "The full output is:", output

You can do this as well:

t = Thread.new { `somecmd` }
...
output = t.value

Thread#value blocks while the thread is running, though.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/