# Execle ruby process from C?

**URL:** <https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922>\
**Category:** Ruby\
**Created:** [March 15, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922 "2009-03-15T20:35:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 15, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/1 "2009-03-15T20:35:55Z")

</div>

Hello, I’m playing with a small http server written in C & Ruby CGI and  
there is something wrong

if(execle("/usr/bin/ruby", “/usr/bin/ruby” " test.rb", NULL, NULL) ==  
-1)  
{  
wlog(“error execle”);  
exit(EXIT\_FAILURE);  
}

- the C program did a chdir to the directory where test.rb can be found.
- ps ae gives  
18133 pts/5 S+ 0:00 /usr/bin/ruby test.rb

test.rb contains just one line -\> print “hello”

But when execle, ruby never returns… I mean, the server can be  
interrupted with a CONTROL/C and gives /usr/bin/ruby test.rb: Interrupt

I don’t understand why…

I have to say that stdout and stdin are redirected to pipes - I tried  
with some unix executables like ‘ls’ and it works

What is the difference with ruby executable ?

Thanks

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 15, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/2 "2009-03-15T20:55:53Z")

</div>

I forgot to say that print ‘hello’ has not effect…

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 16, 2009, 12:04am UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/3 "2009-03-16T00:04:24Z")

</div>

Zouplaz wrote:

> Hello, I’m playing with a small http server written in C & Ruby CGI and  
> there is something wrong
> 
> if(execle("/usr/bin/ruby", “/usr/bin/ruby” " test.rb", NULL, NULL) ==  
> -1)  
> {  
> wlog(“error execle”);  
> exit(EXIT\_FAILURE);  
> }

> What is the difference with ruby executable ?

I may be mistaken because 1. I don’t have your full piece of code or 2.  
I’m not very up-to-date with C.

But simply running …/ruby test.rb and test.rb only has print “hello” in  
it, the output would be to your shell/terminal…not your http server  
o\_O

Correct me if I’m wrong and I’ll try to help the best I can. Post the  
code to your http server and that would probably help alot.

Regards,

- Mac

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 16, 2009, 5:02am UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/4 "2009-03-16T05:02:13Z")

</div>

> The second string is concatenated at compile time into  
> “/usr/bin/ruby test.rb”, so you execute ruby with only that one  
> argument, which is counted as the program name. Therefore ruby  
> waits script from stdin.

The HTTP server shouldn’t be waiting for stdin because test.rb never  
calls for it. The server isn’t returning a value because it isn’t being  
sent to the server, it’s being sent to the shell. If it was waiting for  
input from stdin then hitting enter would return an input  
value…nil…and it would return as such.

- Mac

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 16, 2009, 4:53am UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/5 "2009-03-16T04:53:41Z")

</div>

Hi,

At Mon, 16 Mar 2009 04:32:42 +0900,  
Zouplaz wrote in [ruby-talk:331208]:

> if(execle("/usr/bin/ruby", “/usr/bin/ruby” " test.rb", NULL, NULL) == -1)

The second string is concatenated at compile time into  
“/usr/bin/ruby test.rb”, so you execute ruby with only that one  
argument, which is counted as the program name. Therefore ruby  
waits script from stdin.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [March 16, 2009, 5:53am UTC](https://www.ruby-forum.com/t/execle-ruby-process-from-c/162922/6 "2009-03-16T05:53:18Z")

</div>

Hi,

At Mon, 16 Mar 2009 12:59:51 +0900,  
Michael L. wrote in [ruby-talk:331241]:

> > The second string is concatenated at compile time into  
> > “/usr/bin/ruby test.rb”, so you execute ruby with only that one  
> > argument, which is counted as the program name. Therefore ruby  
> > waits script from stdin.
> 
> The HTTP server shouldn’t be waiting for stdin because test.rb never  
> calls for it.

Correct, but OP’s code doesn’t any arguments to /usr/bin/ruby.  
So ruby expects that the script will be given from stdin.

That is, I meant a bug about string literal concatenation. It  
should be:

if(execle("/usr/bin/ruby", “/usr/bin/ruby”, “test.rb”, NULL, NULL) ==  
-1)

Note that a comma between “/usr/bin/ruby” and “test.rb”, and  
stripping a leading space from " test.rb", also.
