[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

invoke system command from within a method

Moritz Reiter

11/22/2006 5:55:00 PM

-----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 <mreiter@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...

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

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

10 Answers

Paul Lutus

11/22/2006 7:27:00 PM

0

Moritz Reiter 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

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?

--
Paul Lutus
http://www.ara...

wmwilson01

11/23/2006 4:21:00 AM

0

Moritz Reiter 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.

--
Posted via http://www.ruby-....

Moritz Reiter

11/23/2006 9:07:00 AM

0

-----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 :(

Thanks for your answer, anyway.

- --
Moritz <mo@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...
- --
Moritz Reiter <mreiter@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...

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

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

Paul Lutus

11/23/2006 9:34:00 AM

0

Moritz Reiter wrote:

> -----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.

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 :(

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.

--
Paul Lutus
http://www.ara...

Jano Svitok

11/23/2006 4:45:00 PM

0

On 11/22/06, Moritz Reiter <mreiter@agrav.org> 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.

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

Moritz Reiter

11/24/2006 8:17:00 AM

0

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

Jan Svitok 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...

> 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 <mreiter@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...

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

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

Stefano Crocco

11/24/2006 8:39:00 AM

0

Alle 09:16, venerdì 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, 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?

Moritz Reiter

11/24/2006 10:41:00 AM

0

-----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.

Sorry for bugging you all.

Regards,

- --
Moritz Reiter <mreiter@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...

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

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

Jano Svitok

11/24/2006 3:42:00 PM

0

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

Moritz Reiter

11/24/2006 4:21:00 PM

0

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

Jan Svitok wrote:
> 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
>

Cool, thanks for the hint!

- --
Moritz Reiter <mreiter@agrav.org>

GPG key fingerprint:
https://luna.agrav.or...

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

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