# How to Execute simple Linux Command from controller?

**URL:** <https://www.ruby-forum.com/t/how-to-execute-simple-linux-command-from-controller/117951>\
**Category:** Rails\
**Created:** [October 22, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-execute-simple-linux-command-from-controller/117951 "2007-10-22T19:17:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-execute-simple-linux-command-from-controller/117951/1 "2007-10-22T19:17:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/how-to-execute-simple-linux-command-from-controller/117951/2 "2007-10-22T21:16:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 23, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/how-to-execute-simple-linux-command-from-controller/117951/3 "2007-10-23T15:46:40Z")

</div>

removed\_email\_address@domain.invalid 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 🙂

end

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

hope it helps 😛
