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.
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.
$stdout.sync = true
%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.
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:
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)
I suspect stderr might be a problem. Try adding 2>/dev/null
Try adding sh before the command
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.
The documentation is at Kernel#`, and the ultimate documentation is
in /usr/local/src/ruby/io.c, function rb_f_backquote()
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?
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.
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…
I suspect stderr might be a problem. Try adding 2>/dev/null
That does not change the behaviour.
Try adding sh before the command
Neither does this.
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.
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?
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
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?