Jano Svitok
11/24/2006 3:42:00 PM
On 11/24/06, Moritz Reiter <mreiter@agrav.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Moritz Reiter wrote:
> > 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.
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