# Forking a process in Rails is messing up mongrel

**URL:** <https://www.ruby-forum.com/t/forking-a-process-in-rails-is-messing-up-mongrel/81763>\
**Category:** Rails\
**Created:** [January 12, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/forking-a-process-in-rails-is-messing-up-mongrel/81763 "2007-01-12T23:59:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpjolicoeur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cpjolicoeur](https://www.ruby-forum.com/u/cpjolicoeur)\
**Post date:** [January 12, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/forking-a-process-in-rails-is-messing-up-mongrel/81763/1 "2007-01-12T23:59:24Z")

</div>

I’m trying to run an external Ruby script from my Rails app.

To do this i’m using this function

def fork\_with\_new\_connection(config, my\_class = ActiveRecord::Base)  
pid = fork do  
begin  
my\_class.establish\_connection(config)  
yield  
ensure  
my\_class.remove\_connection  
end  
end  
Process.detach(pid)  
end

Then, within my controller I do this:

```
# fork a process
config = ActiveRecord::Base.remove_connection
fork_with_new_connection(config) {
  @environment = (RAILS_ENV == 'development') ? 'dev' : 'prod'
  system("ruby ./public/utils/delicious_import.rb -u #{@user} -p

```

#{@pass} -r #{@rating} -d #{current\_user} -e #{@environment}")  
}  
ActiveRecord::Base.establish\_connection(config)

I have two questions. First, is this the proper/correct way to run an  
external script from a rails controller. I just want to fire off the  
script and continue; I don’t want to wait around for its completion or  
status.

Second, when I do this, my mongrel.pid file seems to be deleted as soon  
as the child process completes. It looks like the child process’s  
parent is a mongrel\_rails process (which makes sense since I am running  
mongrel), but then when the process dies, it also removes the  
mongrel.pid file from my log/ directory.

---

<div class="post-metadata">

**Author:** ![cpjolicoeur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cpjolicoeur](https://www.ruby-forum.com/u/cpjolicoeur)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/forking-a-process-in-rails-is-messing-up-mongrel/81763/2 "2007-01-19T16:30:32Z")

</div>

> I’m trying to run an external Ruby script from my Rails app.
> 
> I have two questions. First, is this the proper/correct way to run an  
> external script from a rails controller. I just want to fire off the  
> script and continue; I don’t want to wait around for its completion or  
> status.

Don’t know about fork, but whenever external scripts come up, everyone  
recommends looking at backgrounddrb: [http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)

-philip

---

<div class="post-metadata">

**Author:** ![cpjolicoeur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@cpjolicoeur](https://www.ruby-forum.com/u/cpjolicoeur)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/forking-a-process-in-rails-is-messing-up-mongrel/81763/3 "2007-01-19T16:31:56Z")

</div>

Craig, please read this:

[http://rubyforge.org/pipermail/mongrel-users/2006-August/001207.html](http://rubyforge.org/pipermail/mongrel-users/2006-August/001207.html)

Not sure if it’s relevant, but might help.

Vish
