# Running a shell command in the background from Rails

**URL:** <https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283>\
**Category:** Rails\
**Created:** [May 16, 2007, 11:14am UTC](https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283 "2007-05-16T11:14:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [May 16, 2007, 11:14am UTC](https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283/1 "2007-05-16T11:14:41Z")

</div>

Hi

I need to call a method on one of my models in response to a form  
being submitted that may take quite some time to run. I’d like to just  
use script/runner and have it run in the background returning  
immediately. The following seems like it should work but instead it  
waits for the command to finish:

```
`#{RAILS_ROOT}/script/runner \"Project.my_method\" &`

```

With the ‘&’ it returns immediately when run from the terminal but not  
when called from Rails.

I’ve had a look at backgroundrb but that seems far too complicated for  
the simple thing I’m trying to achieve. Is there a way I can run a  
shell command like this but have it return immediately?

David N.

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [May 17, 2007, 2:50am UTC](https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283/2 "2007-05-17T02:50:41Z")

</div>

On 5/16/07, David N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> With the ‘&’ it returns immediately when run from the terminal but not  
> when called from Rails.

Try using system() instead of backticks, and be sure to redirect  
stderr and stdout to a file or to /dev/null.

system “#{RAILS\_ROOT}/script/runner Project.my\_method \>/dev/null 2\>&1  
&”

HTH

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [May 17, 2007, 4:27am UTC](https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283/3 "2007-05-17T04:27:49Z")

</div>

Bob S. wrote:

> HTH  
> nohup?

–  
Phlip  
["Pigleg too?", week 1](http://flea.sourceforge.net/PiglegToo_1.html)

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [May 18, 2007, 10:39am UTC](https://www.ruby-forum.com/t/running-a-shell-command-in-the-background-from-rails/104283/4 "2007-05-18T10:39:48Z")

</div>

That works great, many thanks

David
