# Backgroundrb help needed!

**URL:** <https://www.ruby-forum.com/t/backgroundrb-help-needed/174995>\
**Category:** Rails\
**Created:** [September 5, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/backgroundrb-help-needed/174995 "2009-09-05T13:43:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [September 5, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/backgroundrb-help-needed/174995/1 "2009-09-05T13:43:00Z")

</div>

I have what I thought would be a simple thing - we use backgroundrb to  
watch a directory and process new files. We want to make sure that the  
worker is running. I thought I could have a schedule that started the  
worker every 5 minutes to make sure, but, it loads a new one each time  
(not surprising).  
Is there a way to make sure that there is one (and only one) of these  
workers running at all times?

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [September 5, 2009, 7:33pm UTC](https://www.ruby-forum.com/t/backgroundrb-help-needed/174995/2 "2009-09-05T19:33:44Z")

</div>

Hi Phil,

On Sat, 2009-09-05 at 04:41 -0700, phil wrote:

> I have what I thought would be a simple thing - we use backgroundrb to  
> watch a directory and process new files. We want to make sure that the  
> worker is running. I thought I could have a schedule that started the  
> worker every 5 minutes to make sure, but, it loads a new one each time  
> (not surprising).  
> Is there a way to make sure that there is one (and only one) of these  
> workers running at all times?

You’ll probably have better luck with this on the backgroundrb list.

[http://backgroundrb.rubyforge.org/community/](http://backgroundrb.rubyforge.org/community/)

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [September 5, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/backgroundrb-help-needed/174995/3 "2009-09-05T20:37:32Z")

</div>

On Sep 5, 1:32 pm, bill walton [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You’ll probably have better luck with this on the backgroundrb list.
> 
> [http://backgroundrb.rubyforge.org/community/](http://backgroundrb.rubyforge.org/community/)
> 
> HTH,  
> Bill

BackgrounDRb or no, creating a file containing the process’s PID, and  
verifying that each time the process is started is the typical unixy  
way to do this. How I do it:

def self.is\_running?  
if File.exists? pidfile  
begin  
Process.kill 0, pid  
return true  
rescue Errno::EPERM  
puts “You don’t own this process.”  
return true  
rescue Errno::ESRCH #pid doesn’t exist anymore  
delete\_pidfile  
end  
end  
false  
end

def self.delete\_pidfile  
File.delete pidfile  
rescue  
end

def self.pid  
File.read(pidfile).chomp.to\_i rescue nil  
end

def self.pidfile  
“path/to/background\_process.pid”  
end

And of course just create the the appropriate file writing Process.pid  
to it.
