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

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

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

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"}

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

···

On Wed, 3 Jan 2007, Moritz Reiter wrote:
#
# 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
--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama

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! :slight_smile:

# 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

···

ara.t.howard@noaa.gov wrote: