[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Daemonize like fetchmail

Payton Swick

1/5/2006 8:48:00 PM

Has anyone used the Daemons library (http://daemons.ruby...)
extensively? I'm trying to do something that doesn't appear covered by
the examples, and I'm wondering if I need to reinvent the wheel.

What I'd like to do is to easily add functionality to my script similar
to the way that fetchmail works, ie:

When starting the script with one command-line option (eg: --daemon), I
daemonize it and start it running (saving the PID in a file).

When starting the script with another command-line option (eg: --stop),
it finds the PID file and kills the instance.

In a sense, I'd like to combine the wrapper method given in the examples
and the script it runs, but the wrapper example seems to absorb
command-line options, and I want to call those functions from within
Ruby instead.

Suggestions?

-Payton


7 Answers

Ara.T.Howard

1/5/2006 9:27:00 PM

0

Payton Swick

1/10/2006 2:53:00 PM

0

Well, I did it myself anyway. Hope it's useful:
http://rubyforge.org/projec...

-Payton

Payton Swick wrote:
> Has anyone used the Daemons library (http://daemons.ruby...)
> extensively? I'm trying to do something that doesn't appear covered by
> the examples, and I'm wondering if I need to reinvent the wheel.
>
> What I'd like to do is to easily add functionality to my script similar
> to the way that fetchmail works, ie:
>
> When starting the script with one command-line option (eg: --daemon), I
> daemonize it and start it running (saving the PID in a file).
>
> When starting the script with another command-line option (eg: --stop),
> it finds the PID file and kills the instance.
>
> In a sense, I'd like to combine the wrapper method given in the examples
> and the script it runs, but the wrapper example seems to absorb
> command-line options, and I want to call those functions from within
> Ruby instead.
>
> Suggestions?
>
> -Payton


Ara.T.Howard

1/10/2006 3:24:00 PM

0

Payton Swick

1/10/2006 4:51:00 PM

0

Would this be correct usage of the file, then?

def pid
dpid = nil
File.open(pid_file, File::RDONLY) { |file| dpid = file.gets.chomp
if file.flock(File::LOCK_SH|File::LOCK_NB); file.flock(File::LOCK_UN) }
if pid_exists?
dpid.to_i
end


def save_pid
File.open(pid_file, File::CREAT|File::EXCL|File::WRONLY) { |file|
file.puts $$ if file.flock(File::LOCK_EX); file.flock(File::LOCK_UN) }
end

-Payton

ara.t.howard@noaa.gov wrote:
> On Tue, 10 Jan 2006, Payton Swick wrote:
>
>> Well, I did it myself anyway. Hope it's useful:
>> http://rubyforge.org/projec...
>>
>> -Payton
>
>
> it does look useful. few comments/suggestions though
>
> - since it's *nix specify anyhow you might want to use O_EXCL when
> opening
> the file since otherwise there is a race condition to create the file
>
> - on the same page using File#flock with File::LOCK_EX when writing and
> File::LOCK_SH for writing and reading (with File::LOCK_NB of course)
> will
> avoid the race conditions and possibibity of reading a half-baked file
>
> - escalation of signals with a pause between is useful for killing
> processes
> since many trap signit and most do not die immediately. something like
>
> def maim(pid, signals=%w(SIGTERM SIGQUIT SIGKILL), suspend=4)
> pid = Integer("#{ pid }")
> signals = [ signals ].flatten.map{|sig| "#{ sig }"}
> suspend = Integer("#{ suspend }")
>
> existed = false
> sigs.each do |sig|
> begin
> Process::kill(sig, pid)
> existed = true
> rescue Errno::ESRCH
> return(existed ? true : nil)
> end
> return true unless alive?(pid)
> sleep suspend
> return true unless alive?(pid)
> end
> return(not alive?(pid))
> end
>
> which makes sure the process is actually dead prevents removing the
> pidfile in cases where the Process::kill didn't work
>
> - the alive function above can be something like this
>
> def alive pid
> pid = Integer("#{ pid }")
> begin
> Process::kill 0, pid
> true
> rescue Errno::ESRCH
> false
> end
> end
>
> cheers.
>
> -a


Ara.T.Howard

1/10/2006 5:24:00 PM

0

Jos Backus

1/10/2006 5:42:00 PM

0

On Tue, Jan 10, 2006 at 11:53:08PM +0900, Payton Swick wrote:
> Well, I did it myself anyway. Hope it's useful:
> http://rubyforge.org/projec...

Instead of putting this logic in (every) application, have you considered
using daemontools? http://cr.yp.to/daemon...

Personally, I think pid files are evil.

--
Jos Backus
jos at catnook.com


tony summerfelt

1/11/2006 1:01:00 PM

0

Payton Swick wrote on 1/10/2006 9:53 AM:

> Well, I did it myself anyway. Hope it's useful:
> http://rubyforge.org/projec...

it certainly is :)

i was working on something similar, but decided to use your wheel
instead of re-inventing my own.
--
http://home.cogeco.ca/~ts...
telnet://ventedspleen.dyndns.org