How to Execute simple Linux Command from controller?

Folks,

I am new to rails and have not found out how to do this. What I am
trying to do is execute a Linux command from one of my controllers.
For example, I’ve tried these 2 methods. Both result in a the web-page
to hang forever. I get NO response back.

The question I have is, how do I execute a Linux command and then
parse the output from this command to take further action. Much
appreciated.

def restore1
render_text uname -a
end

  or

def restore2
io = IO.popen(“uname -a”)
output = io.read
io.close
end

Pete M.

Not sure if this will help, but render_text is deprecated. Try using
render :text => uname -a

I have successfully been able to execute shell commands and get the
results back from within a Rails app, but I put them in a separate model
class and returned the results in a method.

[email protected] wrote:

Folks,

I am new to rails and have not found out how to do this. What I am
trying to do is execute a Linux command from one of my controllers.
For example, I’ve tried these 2 methods. Both result in a the web-page
to hang forever. I get NO response back.

The question I have is, how do I execute a Linux command and then
parse the output from this command to take further action. Much
appreciated.

def restore1
render_text uname -a
end

  or

def restore2
io = IO.popen(“uname -a”)
output = io.read
io.close
end

Pete M.

u may need to require + include the file into the controller before
being able to use it’s functionalities, ie:

require ‘io’
class ExController

include IO

now i can use the io.rb file’s methods :slight_smile:

end

(the above very untested, i’m not sure exactly what file needs to be
included, but this is the idea)

hope it helps :stuck_out_tongue:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs