how can I do a linux mount in a programmatic way ?

J

Jedrin

If I do something like system('sudo mount .. ') then I don't really
get any error codes back to see what happened because I just get the
sudo return code here. Is there a way to do this like a library call
in ruby ?
 
I

Iñaki Baz Castillo

El Martes, 27 de Octubre de 2009, Jedrin escribi=F3:
If I do something like system('sudo mount .. ') then I don't really
get any error codes back to see what happened because I just get the
sudo return code here. Is there a way to do this like a library call
in ruby ?
=20

Use open4 (gem install open4):

=2D------------------
require "open4"

pid, stdin, stdout, stderr =3D Open4::popen4("sudo mount...")
ignored, status =3D Process::waitpid2 pid

if status.to_i !=3D 0
puts "Error: Command returned #{status.to_i}""
end
=2D--------------------

You can also inspect the output and error output reading stdout and stderr=
=20
(they are IO objects, no strings).

=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
B

Brian Candler

Jedrin said:
If I do something like system('sudo mount .. ') then I don't really
get any error codes back to see what happened because I just get the
sudo return code here.

I think you should get the exit status of the program which sudo ran:

$ cat xxx.sh
exit 123
$ irb --simple-prompt
[Ubuntu Hardy, sudo 1.6.9p10-1ubuntu3.5]

Otherwise, I don't know if there's a way to call the mount API directly
from ruby.

Even if there was, remember that it wouldn't work unless the ruby script
itself were running as root. That is: if you need 'sudo' to get root as
in your original example, then it's always going to be in a separate
process.
 

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
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top