# Wait on external program?

**URL:** <https://www.ruby-forum.com/t/wait-on-external-program/194864>\
**Category:** Ruby\
**Created:** [September 12, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/wait-on-external-program/194864 "2010-09-12T17:57:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![larryj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@larryj](https://www.ruby-forum.com/u/larryj)\
**Post date:** [September 12, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/wait-on-external-program/194864/1 "2010-09-12T17:57:35Z")

</div>

Launching external .exe (windows) using:

IO.popen(appname)

I need to wait for that app to finish(close) so I can use its results.

Any help?

Thanks.

---

<div class="post-metadata">

**Author:** ![larryj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@larryj](https://www.ruby-forum.com/u/larryj)\
**Post date:** [September 12, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/wait-on-external-program/194864/2 "2010-09-12T18:23:16Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 12.09.2010 17:57, schrieb Larry J.:

> Launching external .exe (windows) using:
> 
> IO.popen(appname)
> 
> I need to wait for that app to finish(close) so I can use its results.
> 
> Any help?
> 
> Thanks.

What about using backticks?

result = `appname`

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.10 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iEYEARECAAYFAkyM/mIACgkQDYShvwAbcNllKwCgkPK2cNGvicsRoo8IhDJFZU6h  
EtMAniELVg9uZ7Bz7Oxy3QUA8WMEfWWy  
=ZRwF  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![larryj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@larryj](https://www.ruby-forum.com/u/larryj)\
**Post date:** [September 12, 2010, 7:22pm UTC](https://www.ruby-forum.com/t/wait-on-external-program/194864/3 "2010-09-12T19:22:20Z")

</div>

Marvin,

Thanks for the suggestion; however, I donâ€™t understand how this will  
force Ruby to wait until the external process is complete.

Are you suggesting some sort of while… loop waiting for the variable to  
change?

The external process is not Ruby code but a windows application.

Thanks,  
Larry

---

<div class="post-metadata">

**Author:** ![larryj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@larryj](https://www.ruby-forum.com/u/larryj)\
**Post date:** [September 12, 2010, 10:21pm UTC](https://www.ruby-forum.com/t/wait-on-external-program/194864/4 "2010-09-12T22:21:32Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 12.09.2010 19:26, schrieb Larry J.:

> Thanks,  
> Larry

The backtick method (yeah, that’a a method, see  
[http://www.ruby-doc.org/ruby-1.9/classes/Kernel.html#M002609](http://www.ruby-doc.org/ruby-1.9/classes/Kernel.html#M002609) ) executes  
the command you pass, waits for it to finish and captures the output  
emmited by the command. I’m on Linux Ubuntu at the moment, but this  
example should explain it:

irb(main):001:0\> str = `sleep 2; echo hello`  
=\> “hello\n”  
irb(main):002:0\>

I use sleep to simulate a long-running operation (well, 2 seconds  
lasting 😉 ) followed by outputting hello. The “hello” gets captured by  
the backticks method and I assign it to the variable str.

The Kernel#system method is similar to the backticks (it waits for the  
process to finish as well), but it doesn’t capture the output.

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.10 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iEYEARECAAYFAkyNNg8ACgkQDYShvwAbcNnQnwCfRqT+YHMXWv/+2sNdItd42lx+  
KkIAnjj6OOc6uLH37Q7rZtjKHc7US+dX  
=tpVH  
-----END PGP SIGNATURE-----
