# "su user -c 'command'" from within ruby

**URL:** <https://www.ruby-forum.com/t/su-user-c-command-from-within-ruby/80334>\
**Category:** Ruby\
**Created:** [December 24, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/su-user-c-command-from-within-ruby/80334 "2006-12-24T13:55:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![crytyca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@crytyca](https://www.ruby-forum.com/u/crytyca)\
**Post date:** [December 24, 2006, 1:55pm UTC](https://www.ruby-forum.com/t/su-user-c-command-from-within-ruby/80334/1 "2006-12-24T13:55:47Z")

</div>

Hi,

I want to invoke a shell command from within ruby but as another user in  
a Linux environment. I start the ruby script itself as root and in the  
script I do something like this:

%x{su some\_user -c “some\_command”}

some\_more\_ruby\_stuff

The problem is: ‘some\_command’ gets successfully executed, but the ruby  
script seems to never return to normal operation. ‘some\_more\_ruby\_stuff’  
gets never executed.

Could anyone tell me why this happens and maybe what I could do about  
it?

Regards,  
Moritz

---

<div class="post-metadata">

**Author:** ![crytyca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3be4f8/32.png) [@crytyca](https://www.ruby-forum.com/u/crytyca)\
**Post date:** [December 24, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/su-user-c-command-from-within-ruby/80334/2 "2006-12-24T14:21:48Z")

</div>

Moritz R. wrote:

> The problem is: ‘some\_command’ gets successfully executed, but the ruby  
> script seems to never return to normal operation. ‘some\_more\_ruby\_stuff’  
> gets never executed.
> 
> Could anyone tell me why this happens and maybe what I could do about it?
> 
> Regards,  
> Moritz

It works just fine here. I tried running this:

```
puts %x{su wvdschel -c "whoami"}
puts "test"

```

which worked just fine:

```
root@wvdschel-laptop:~# ruby test
wvdschel
test

```

Are you sure your command returns? Maybe it blocks until the process  
ends? Is there any information you could give on the command you are  
trying to execute?

Wim

–  
Wim Vander S.  
Bachelor Computer Science, University Ghent

[http://nanoblog.ath.cx](http://nanoblog.ath.cx)  
My weblog, powered by Ruby and BSD licensed.
