# System waiting for launched process AND forked processes

**URL:** <https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630>\
**Category:** Ruby\
**Created:** [February 17, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630 "2010-02-17T18:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/1 "2010-02-17T18:22:07Z")

</div>

Hello,

We have written a small client-server program in c/socket. To launch the  
server, we call the program which internally fork. The original process  
returns immediately but the forked process stays alive and listen to  
incoming socket connection.  
It’s all fine when running this from bash (we’re on ubuntu) but when  
using ruby (with either system, backtick or #x) it’s blocking until both  
the original process and the forked process terminate.

Is it what we should expect when calling system? If yes, how could we  
work around that?

Thanks

---

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 6:42pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/2 "2010-02-17T18:42:36Z")

</div>

> It’s all fine when running this from bash (we’re on ubuntu) but when  
> using ruby (with either system, backtick or #x) it’s blocking until both  
> the original process and the forked process terminate.
> 
> Is it what we should expect when calling system? If yes, how could we  
> work around that?

Mine doesn’t seem to do that.

rdp@li49-39:~$ cat spawn.rb  
Process.fork {  
puts ‘in daemon’  
puts Process.pid  
sleep  
}  
puts ‘terminating’

rdp@li49-39:~$ cat spawn2.rb  
system("~/installs/ruby\_trunk\_installed/bin/ruby spawn.rb")  
puts ‘done’

rdp@li49-39:~$ ruby -v spawn2.rb  
ruby 1.8.6 (2009-3-4 mbari 8B/0x8770 on patchlevel 287) [i686-linux]  
terminating  
done  
in daemon  
3199  
rdp@li49-39:~$ ps -ef | grep 3199  
rdp 3199 1 0 17:41 pts/0 00:00:00  
/home/rdp/installs/ruby\_trunk\_installed/bin/ruby spawn.rb  
rdp 3272 477 0 17:41 pts/0 00:00:00 grep 3199

(3199 is still alive).

Perhaps your other process.wait’ing on the first or something?

One thing that might help (in 1.9)  
Process.daemon {

# something

}

or

Process.fork {  
Thread.new { system(“long running command”)}  
}

or what not.

GL.  
-rp

---

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/3 "2010-02-17T18:50:49Z")

</div>

On 02/17/2010 06:22 PM, Pierre Morel wrote:

> We have written a small client-server program in c/socket. To launch the  
> server, we call the program which internally fork. The original process  
> returns immediately but the forked process stays alive and listen to  
> incoming socket connection.  
> It’s all fine when running this from bash (we’re on ubuntu) but when  
> using ruby (with either system, backtick or #x) it’s blocking until both  
> the original process and the forked process terminate.
> 
> Is it what we should expect when calling system? If yes, how could we  
> work around that?

Hmmm, might be that you have an issue because your child’s child is  
attached to your ruby process once the starter process stops. There is  
a whole range of other possible issues causing this which is hard to  
sort out given the little information we have. Did you take proper  
measures to demonize your child process after fork?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/4 "2010-02-17T19:05:28Z")

</div>

On 02/17/2010 06:42 PM, Roger P. wrote:

> Process.fork {  
> Thread.new { system(“long running command”)}  
> }

What do you create the thread for? Also exec is probably a better  
choice in this case.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 8:32pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/5 "2010-02-17T20:32:05Z")

</div>

> > Process.fork {  
> > Thread.new { system(“long running command”)}  
> > }
> 
> What do you create the thread for? Also exec is probably a better  
> choice in this case.

ahh yes exec would work well.  
-r

---

<div class="post-metadata">

**Author:** ![pmorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pmorel](https://www.ruby-forum.com/u/pmorel)\
**Post date:** [February 17, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/system-waiting-for-launched-process-and-forked-processes/183630/6 "2010-02-17T22:23:57Z")

</div>

Robert K. wrote:

> Hmmm, might be that you have an issue because your child’s child is  
> attached to your ruby process once the starter process stops. There is  
> a whole range of other possible issues causing this which is hard to  
> sort out given the little information we have. Did you take proper  
> measures to demonize your child process after fork?
> 
> Kind regards
> 
> robert

I didn’t take any measure to daemonize the child… Now I have done it  
(a call to setsid and few other things, I used the example at  
[http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize](http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize)) and it’s  
working perfectly.

My mistake was assume that it would work from ruby if it was working  
fine from the shell or even from another C++ program.

Thank you very much for your quick response.

Pierre
