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

M

Moritz Reiter

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
 
W

Wim Vander Schelden

Moritz said:
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://nanoblog.ath.cx
My weblog, powered by Ruby and BSD licensed.
 
M

Moritz Reiter

Wim, thanks for your reply and sorry that it took me so long to reply again.
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"}
 
A

ara.t.howard

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


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 != ""

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

#
# use Net::SSH
#
# 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.

regards.

-a
 
M

Moritz Reiter

#
# use Process.kill
#


#
# use Net::SSH
#

That makes me look like a complete newbie, eh? Actually I am. So thanks
for the hints! :)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top