[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Moritz Reiter

12/24/2006 12:51:00 PM

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
4 Answers

Wim Vander Schelden

12/24/2006 1:20:00 PM

0

Moritz Reiter wrote:
> 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
>
>
>
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 Schelden
Bachelor Computer Science, University Ghent

http://nanob...
My weblog, powered by Ruby and BSD licensed.


Moritz Reiter

1/2/2007 5:57:00 PM

0

Wim, thanks for your reply and sorry that it took me so long to reply again.

Wim Vander Schelden wrote:
> 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?

This is what I want to do:

port = 24800
synergy_client = "synergyc"
pattern = "^ssh.*#{port}"

pid = %x{pgrep -f #{pattern}}
%x{kill #{pid}} if pid != 0 and pid != nil and pid != ""

cmd = "su #{USER} -c \"ssh -f -N -L #{port}:#{@site.synergy_server}:#{port} #{@site.synergy_server}\""

# this one gets executed but nothing afterwards...
%x{#{cmd}}

pid = %x{pgrep -f #{synergy_client}}
%x{kill #{pid}} if pid != 0 and pid != nil and pid != ""

%x{su #{USER} -c "#{synergy_client} localhost"}

Ara.T.Howard

1/2/2007 6:25:00 PM

0

Moritz Reiter

1/2/2007 8:49:00 PM

0

ara.t.howard@noaa.gov wrote:
>> pid = %x{pgrep -f #{pattern}}
>> %x{kill #{pid}} if pid != 0 and pid != nil and pid != ""
>
> #
> # use Process.kill
> #
>
>>
>> cmd = "su #{USER} -c \"ssh -f -N -L >> #{port}:#{@site.synergy_server}:#{port} #{@site.synergy_server}\""
>
> #
> # use Net::SSH
> #

That makes me look like a complete newbie, eh? Actually I am. So thanks
for the hints! :)

>>
>> # this one gets executed but nothing afterwards...
>> %x{#{cmd}}
>
> this is probably blocking and asking for password. perhaps you don't have
> keys or your agent running properly? because you are using backticks/%x
> any
> stdout, such as a password prompt, will be lost. just by changing this to
> 'system' you should be able to see if this is the case.
>
> in any case i'm guessing this is a env/ssh issue rather than a ruby one.

It works when I use 'system' instead of %x{} but it doesn't prompt for a
password either. I have the public ssh keys exchanged. So it most
probably is an environment issue. In any case: You helped me a lot,
thank you!

Regards,
Moritz