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?
Hi Phil,
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.