views:

488

answers:

2

I'm using Ubuntu 9.04 x64 and,

I have a file startup.rb in which I call sudo bash, so that I always have a root console to do administrative tasks without typing password after every 15 minutes or so.

This script is called by another script Startup.rb, and content of both files are like this -

File ~/Startup.rb

#!/usr/bin/ruby
system "gnome-terminal --maximize -x ruby ~/startup.rb"

File ~/startup.rb

#!/usr/bin/ruby
`sudo some-repetitive-administrative-task`
....
....
`sudo bash` #Not using `sudo -i`, since that makes `pwd` -> /root

I have included the ~/Startup.rb file in Startup Applications list.

Now the problem is that, in the terminal of sudo bash, if I type something and expect some output, I don't get any. So if I write echo hello world, I don't get any output. Which leads me to believe that the standard output (stdout) of the sudo bash command is not the console.

So, I want to know why is this happening? How may I know the current stdout path? Or how can I restore stdout back to my current console?

-- thanks

+5  A: 

You're using an inapproriate method to run system commands from Ruby. Try this instead:

#!/usr/bin/ruby

system 'bash'

The syntax you're using (with the backticks) captures the standard output of the command and returns it in a string. That's why you don't see it on the terminal.

Here's a nice review of the different ways to run commands from Ruby: 6 Ways to Run Shell Commands in Ruby.

Ville Laurikari
Well that's frustrating, because I always used system("") until recently when I discovered ``.I tried almost all ways I could, and did I just missed trying system()?Even in the same file `startup.rb`, every other command is executed using system() except this one as I had added this line recently.That's insane.
Vikrant Chaudhary
+1  A: 

If you are only interested in an easier way to run administrative tasks as root, there might be a few other things you might consider.

$sudo -s

This will give you a shell in which you can submit commands as your sudo'd self (assuming that sudo has been setup so that you can run a shell via sudo).

Another thing you can do, though not always recommended or considered good form in Ubuntu is to create a root account:

$sudo passwd root

then you can login as root and administer things that way.

I know this doesn't answer your specific question about ruby, but I hope you find it helpful.

Bryan Ward
Thanks for your input. I'd prefer `sudo -s` over `sudo bash` now.
Vikrant Chaudhary