Invoke system command from within a method

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

I am new to Ruby and want to make a Ruby script for the network
configuration of my notebook as an exercise. Now I encountered a problem
which is pretty strange to me:

I want to invoke ‘/etc/init.d/networking restart’ from within my Ruby
program. If I create a file with the line

%x{ /etc/init.d/networking restart }

it works like I would expect. The networking init script gets invoked
and correctly processed.

But if I do something like this:

  • –>8–
    class Netconf
    def restart
    puts %x{ /etc/init.d/networking restart }
    end
    end

netconf = Netconf.new
netconf.restart

  • –8<–

I get some output from the init script but it doesn’t seem to actually
do anything. It does not reconfigure my eth’s in oppostion to the
behaviour described above if don’t make the system call from within a
method.

I would be very glad if anyone could give me a hint or point me to some
documentation to enlighten me.

Best Regards,


Moritz R. [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZI7awR2rA+A/LU4RAum0AJ9N3mnRAxGlFi1D3afeQbSa0l21nwCgimn3
RnvNFcgWi+OLcngGIOKO2jU=
=W+gv
-----END PGP SIGNATURE-----

Moritz R. wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

I am new to Ruby and want to make a Ruby script for the network
configuration of my notebook as an exercise. Now I encountered a problem
which is pretty strange to me:

I want to invoke ‘/etc/init.d/networking restart’ from within my Ruby
program. If I create a file with the line

%x{ /etc/init.d/networking restart }

it works like I would expect. The networking init script gets invoked
and correctly processed.

But if I do something like this:

  • –>8–
    class Netconf
    def restart
    puts %x{ /etc/init.d/networking restart }
    end
    end

netconf = Netconf.new
netconf.restart

  • –8<–

I get some output from the init script but it doesn’t seem to actually
do anything. It does not reconfigure my eth’s in oppostion to the
behaviour described above if don’t make the system call from within a
method.

I would be very glad if anyone could give me a hint or point me to some
documentation to enlighten me.

Well, 2 things that I’d try, though I have no idea if either will
actually help.

  1. $stdout.sync = true
  2. %x{/etc/init.d/networking restart </dev/tty}

Both long-shots, but I can’t think of another reason why you’d be having
a problem.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El Gato wrote:

Well, 2 things that I’d try, though I have no idea if either will
actually help.

  1. $stdout.sync = true
  2. %x{/etc/init.d/networking restart </dev/tty}

Both long-shots, but I can’t think of another reason why you’d be having
a problem.

I tried both of your suggestions, and even both at once. Neither did
work. I don’t have a clue what’s going on :frowning:

Thanks for your answer, anyway.


Moritz [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/


Moritz R. [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZWStwR2rA+A/LU4RAlXzAKC2FrWdYIVvAzdL3LjulBV6cb5b1QCfe40+
mhZYeokKbqbmRY0jBSo/0LQ=
=5LiK
-----END PGP SIGNATURE-----

On 11/22/06, Moritz R. [email protected] wrote:

end
I would be very glad if anyone could give me a hint or point me to some
documentation to enlighten me.

More blind tips:

  1. try adding code to that %x{…} in smaller chunks:
    (without class & puts, add method) def restart ; %x{…} ; end
    (without method and class, add puts)
    (add puts and method)

  2. I suspect stderr might be a problem. Try adding 2>/dev/null

  3. Try adding sh before the command

  4. Have a look at the actual script and see what could be a problem.
    Optionally add some traces there to see how far does it get.

  5. The documentation is at Kernel#`, and the ultimate documentation is
    in /usr/local/src/ruby/io.c, function rb_f_backquote() :wink:

Alle 09:16, venerdì 24 novembre 2006, Moritz R. ha scritto:

if put the
very same statment ( %x{ /etc/init.d/networking restart } into a
separate file as the only statement of the file (besides the hash
bang…) and execute this file, the init script does reconfigure my eth
correctly. I am mostly greatly confused…

Doesn’t it work even if you add lines only after the %x{} line? I mean,
does
something like

#!/usr/bin/ruby
%x{ /etc/init.d/networking restart }
some other code

work or not?

So I suspect that the problem is connected more to the ifdown/ifup binaries
than to the init script itself.

Have you tried running another command from your script in the same way?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Moritz R. wrote:

end
I would be very glad if anyone could give me a hint or point me to some
documentation to enlighten me.

Best Regards,

Okay, now I know what went wrong. I should have told you the whole
story: In my script I rewrite /etc/network/interfaces and then invoke
the init script. My fault was, that I didn’t close the File handler for
/etc/network/interfaces before starting the init script. So the init
script invoked ifdown and ifup but ruby did not write the new content of
/etc/network/interfaces to the harddisk yet, so ifup had no useful data
to do its job.

Damn, I learned my lesson: Always close your files as soon as you have
finisehd writing to them.

Sorry for bugging you all.

Regards,


Moritz R. [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZswdwR2rA+A/LU4RAqhfAKCqa8K1Y/6v+JgwNQb97Mw43BpfHACfXqe7
uuSMcnDydUx7Afrs1bczB8U=
=mqzT
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jan S. wrote:

More blind tips:

  1. try adding code to that %x{…} in smaller chunks:
    (without class & puts, add method) def restart ; %x{…} ; end
    (without method and class, add puts)
    (add puts and method)

Okay, I tried a few cases: The init script does not get processed
correctly no matter if it’s in a class or just in a method or just at
very end of my whole script without puts or anything. BUT if put the
very same statment ( %x{ /etc/init.d/networking restart } into a
separate file as the only statement of the file (besides the hash
bang…) and execute this file, the init script does reconfigure my eth
correctly. I am mostly greatly confused…

  1. I suspect stderr might be a problem. Try adding 2>/dev/null

That does not change the behaviour.

  1. Try adding sh before the command

Neither does this.

  1. Have a look at the actual script and see what could be a problem.
    Optionally add some traces there to see how far does it get.

I looked into the script already. It does not much more than calling

ifdown -a --exclude=lo
ifup -a --exclude=lo

and some logging.

I added ‘set -x’ to the beginning of the init script and the output does
not look different. Only difference is that in once case my eth gets the
new IP, in the other it doesn’t. So I suspect that the problem is
connected more to the ifdown/ifup binaries than to the init script
itself.

  1. The documentation is at Kernel#`, and the ultimate documentation is
    in /usr/local/src/ruby/io.c, function rb_f_backquote() :wink:

ri Kernel#` does not reveal much info. Will have a look into API
documentation or something.
I don’t have much hope to understand much of io.c as I never coded a
single line of C :-/

Maybe you or someone else have another idea after my information update?


Moritz R. [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZqpFwR2rA+A/LU4RAgmdAKCd5zpyJ+1Lied4mmm5v/I0Zl8W0wCfRCTk
6+r8DbO6x9gZaDjulmqGrmg=
=Ncj8
-----END PGP SIGNATURE-----

On 11/24/06, Moritz R. [email protected] wrote:

I want to invoke ‘/etc/init.d/networking restart’ from within my Ruby
class Netconf
do anything. It does not reconfigure my eth’s in oppostion to the
story: In my script I rewrite /etc/network/interfaces and then invoke
the init script. My fault was, that I didn’t close the File handler for
/etc/network/interfaces before starting the init script. So the init
script invoked ifdown and ifup but ruby did not write the new content of
/etc/network/interfaces to the harddisk yet, so ifup had no useful data
to do its job.

Damn, I learned my lesson: Always close your files as soon as you have
finisehd writing to them.

This is where the block variant of File.open comes handly - you don’t
have to close the file even in the case of exception.

File.open(‘blabla’, ‘w’) do |f|
f.write(‘blabla’)
end

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jan S. wrote:

which is pretty strange to me:

This is where the block variant of File.open comes handly - you don’t
have to close the file even in the case of exception.

File.open(‘blabla’, ‘w’) do |f|
f.write(‘blabla’)
end

Cool, thanks for the hint!


Moritz R. [email protected]

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZxvJwR2rA+A/LU4RAlH/AKCNBlmcLifFFl1CY1G+jiYg7svZUgCfUsLr
JWHqkY2D8/db3iuF8d3LJyU=
=fNXp
-----END PGP SIGNATURE-----

Moritz R. wrote:

program. If I create a file with the line
def restart
puts %x{ /etc/init.d/networking restart }
end
end

netconf = Netconf.new
netconf.restart

  • –8<–

I get some output from the init script

What output?

but it doesn’t seem to actually
do anything.

It should have worked, unless of course you were not root. By the way,
running this script as root is (1) a very bad idea, and (2) essential to
make it work.

Were you root? What if anything was printed by the method? Is the above
listing an exact copy of your code?

It does not reconfigure my eth’s

Restarting the network doesn’t reconfigure anything. What did you expect
to
happen, what happened instead, how do they differ?

in oppostion to the
behaviour described above if don’t make the system call from within a
method.

Are you sure about that? Have you tried stopping your network in
advance,
then running the script as root, just to see if the network comes back
up?

Again, what was printed out when the script ran?

Moritz R. wrote:

Both long-shots, but I can’t think of another reason why you’d be
having a problem.

I tried both of your suggestions, and even both at once. Neither did
work.

How do you know they didn’t work? What error messages are being printed?
What happens? What doesn’t happen?

I don’t have a clue what’s going on :frowning:

Neither do we, but if you would tell us what does happen, we might be
able
to help you.

In your first post, you said something was printed out when you ran your
script. What was it?

Did you try running your command as root? Because of the nature of the
command you are issuing, this is a requirement.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs