invoke system command from within a method

M

Moritz Reiter

-----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 Reiter <[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-----
 
E

El Gato

Moritz said:
-----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.
 
M

Moritz Reiter

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


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

Thanks for your answer, anyway.

- --
Moritz <[email protected]>

GPG key fingerprint:
https://luna.agrav.org/~mo/gpg/
- --
Moritz Reiter <[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-----
 
J

Jan Svitok

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.

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() ;-)
 
M

Moritz Reiter

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

Jan said:
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...
2. I suspect stderr might be a problem. Try adding 2>/dev/null

That does not change the behaviour.
3. Try adding sh before the command

Neither does this.
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.

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.
5. The documentation is at Kernel#`, and the ultimate documentation is
in /usr/local/src/ruby/io.c, function rb_f_backquote() ;-)

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 Reiter <[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-----
 
S

Stefano Crocco

Alle 09:16, venerd=EC 24 novembre 2006, Moritz Reiter 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, doe=
s=20
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 binari=
es=20
than to the init script itself.

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

Moritz Reiter

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

Moritz said:
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,

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 Reiter <[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-----
 
J

Jan Svitok

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



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.

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
 
M

Moritz Reiter

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

Jan said:
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 Reiter <[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-----
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top