# Question on exec subprocesses

**URL:** <https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416>\
**Category:** Ruby\
**Created:** [September 29, 2006, 1:08am UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416 "2006-09-29T01:08:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [September 29, 2006, 1:08am UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416/1 "2006-09-29T01:08:13Z")

</div>

Folks,

Can someone shed some light on how I can accomplish something similar  
to this:

exec(“foo action”)  
exec(“foobar action”)  
Process.wait

I want to wait on the second process, yet in ruby 184, all processes  
are waited on (until completion). How can I effectively fork off these  
process simultaneously and wait for the last one to complete or what  
would be really nice, is to wait for both of these separate processes to  
finish.

TIA!  
Phy

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [September 29, 2006, 1:46am UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416/2 "2006-09-29T01:46:17Z")

</div>

On 9/28/06, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Folks,
> 
> Can someone shed some light on how I can accomplish something similar to this:
> 
> exec(“foo action”)  
> exec(“foobar action”)  
> Process.wait
> 
> I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

Does:  
fork {exec “foo action”}  
fork {exec “foobar action”}  
Process.waitall

do what you want?

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [September 29, 2006, 1:52am UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416/3 "2006-09-29T01:52:18Z")

</div>

On 9/28/06, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Folks,
> 
> Can someone shed some light on how I can accomplish something similar to this:
> 
> exec(“foo action”)  
> exec(“foobar action”)  
> Process.wait
> 
> I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

First of all, exec replaces the current process with an external  
command.  
Also Process.wait (if you got to it, which you wouldn’t after exec)  
wait’s for any child process to terminate.

Now assuming that “foo action” and “foobar action” are ruby  
expressions and not external commands I thing you want something like  
this:

child\_pid1 = fork { foo action }  
child\_pid2 = fork {foobar action}

child\_pid1 and child\_pid2 will be the process id’s of the respective  
children.

Now you can do a variety of things as far as waiting for the processes  
to end including:

```
# wait for any child to exit and return it's process id
child_pid = Process.wait

# wait for all children to exit and return an array of the form:
# [[pid1, status1], ...]
status_arrays = Process.wait_all

# wait for any child to exit and return an array containing the 

```

child  
# processes pid and status  
status\_array = Process.wait2

```
 # wait for a specific child process
  waitpid(child_pid1)

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [September 29, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416/4 "2006-09-29T19:19:21Z")

</div>

Thanks, this works like a charm!

----- Original Message ----  
From: Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, September 28, 2006 4:45:44 PM  
Subject: Re: Question on exec subprocesses

On 9/28/06, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Folks,
> 
> Can someone shed some light on how I can accomplish something similar to this:
> 
> exec(“foo action”)  
> exec(“foobar action”)  
> Process.wait
> 
> I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

Does:  
fork {exec “foo action”}  
fork {exec “foobar action”}  
Process.waitall

do what you want?

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [September 29, 2006, 2:34am UTC](https://www.ruby-forum.com/t/question-on-exec-subprocesses/70416/5 "2006-09-29T02:34:20Z")

</div>

Well, I guess I should give more detail, the process I want to fork are  
external commands. Following a post from another help individual, I  
assume I shoul do something like this:

pida = fork { exec “command a;command b” }  
pidb = fork { exec “command a;command b” }  
Process.waitall

Something along these lines?

TIA,  
Phy

----- Original Message ----  
From: Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, September 28, 2006 4:51:29 PM  
Subject: Re: Question on exec subprocesses

On 9/28/06, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Folks,
> 
> Can someone shed some light on how I can accomplish something similar to this:
> 
> exec(“foo action”)  
> exec(“foobar action”)  
> Process.wait
> 
> I want to wait on the second process, yet in ruby 184, all processes are waited on (until completion). How can I effectively fork off these process simultaneously and wait for the last one to complete or what would be really nice, is to wait for both of these separate processes to finish.

First of all, exec replaces the current process with an external  
command.  
Also Process.wait (if you got to it, which you wouldn’t after exec)  
wait’s for any child process to terminate.

Now assuming that “foo action” and “foobar action” are ruby  
expressions and not external commands I thing you want something like  
this:

child\_pid1 = fork { foo action }  
child\_pid2 = fork {foobar action}

child\_pid1 and child\_pid2 will be the process id’s of the respective  
children.

Now you can do a variety of things as far as waiting for the processes  
to end including:

```
# wait for any child to exit and return it's process id
child_pid = Process.wait

# wait for all children to exit and return an array of the form:
# [[pid1, status1], ...]
status_arrays = Process.wait_all

# wait for any child to exit and return an array containing the 

```

child  
# processes pid and status  
status\_array = Process.wait2

```
 # wait for a specific child process
  waitpid(child_pid1)

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
