On 13.12.2009 00:51, Iñáki Baz Castillo wrote: > El SáÃado, 12 de Diciembre de 2009, Robert Klemme escribi >> This is what I would do: >> >> VAR='/var/run' >> >> pid_file = File.join(VAR, File.basename($0) + '.pid') >> >> begin >> old_pid = File.read(pid_file).to_i >> Process.kill 0, old_pid >> $stderr.puts "ERROR: already running with PID #{old_pid}" >> exit 1 >> rescue Errno::ENOENT >> # no pid file >> rescue Errno::ESRCH >> # process not running >> $stderr.puts 'WARNING: stale pid file' >> end >> >> # open file with EXCL in order to catch race conditions where two >> # processes were started almost at the same time and both thought >> # they would be a new instance. Uncomment sleep to simulate: >> # sleep 2 >> File.open(pid_file, File::EXCL | File::WRONLY | File::CREAT) {|io| >> io.write($$)} >> trap(0) {File.delete pid_file} >> >> puts "OK, here we go." > > > Let me a question: is the above a fragment of a Linux init script written in > Ruby (rather than bash/sh as usual)? That was hacked together for demonstration purposes. It was not intended as an init script but rather as the demon itself - albeit without all the demonization stuff (detaching from terminal etc.) because I wanted to focus on the aspect of pid file handling and killing.> If so, according to LSB specifications it should return 0 (success) in case of > "start" action when the daemon is already running. Well, if you write a start script for that "ruby demon" you would have to handle exit codes properly. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/