daemonize, redirect stdin and stdout and write a pidfile

Discussion in 'Ruby' started by Guido De Rosa, Mar 8, 2010.

  1. Hi!

    the subject is a very standard requirement and should be done in a very
    standard way: through ruby core, stdlib or some very popular gem, like
    Daemons ( http://daemons.rubyforge.org/ )

    Anyway, I don't know how to do something like this:

    # THIS CODE IS NOT VALID AND CERTAINLY WILL FAIL
    require 'daemons'
    require 'myclass'

    options = {
    :pidfile => '/var/run/myprog.pid'
    :stdout_to => '/var/log/myprog/myprog.log'
    :stderr_to => '/var/log/myprog/myprog.err'
    }

    Daemons.call(options) do
    MyClass.run! # contains an endless event loop
    end

    Unfortunately, such options do not exist :-(

    How to implement what is expressed in my pseudo-code?

    BTW, I have rolled-my-own temporary solution, but I'm loking for
    something more robust/standard:

    pid = fork do
    # redirects to logfiles
    STDOUT.reopen(config[:stdout_to], 'w')
    STDERR.reopen(config[:stderr_to], 'w')

    # buffered I/O on log files, sync them upon request!
    Signal.trap('USR1') do
    STDOUT.flush
    STDERR.flush
    end

    MyClass.run! # traps SIGINT/SIGTERM and should exit gacefully

    # the child process remove the pidfile on exit
    FileUtils.rm config[:pidfile] if File.exists? config[:pidfile]
    end

    # the parent process writes the pidfile
    if config[:pidfile]
    File.open config[:pidfile], 'w' do |f|
    f.write pid
    end
    end


    Thanks for any suggestion!

    Guido
    --
    Posted via http://www.ruby-forum.com/.
     
    Guido De Rosa, Mar 8, 2010
    #1
    1. Advertising

  2. Have you checked out the documentation of the PidFile class in the
    Daemons gem? It implements options :dir_mode=>:dir,
    :dir=>"/path/to/piddir"

    Also, you can pass a filename as the first argument to daemonize().
    However, both STDOUT and STDERR are written to the same file.

    I found this out just through a little grepping.

    cd /var/lib/gems/1.8/gems/daemons-1.0.10/
    grep -iR pidfile .
    grep -iR stderr .
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Mar 9, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Rudin

    python-daemonize and upstart

    Paul Rudin, Nov 14, 2009, in forum: Python
    Replies:
    3
    Views:
    2,386
    Paul Rudin
    Nov 14, 2009
  2. babbu Pehlwan

    How to daemonize a HTTPServer

    babbu Pehlwan, Sep 1, 2011, in forum: Python
    Replies:
    1
    Views:
    208
  3. Payton Swick

    Daemonize like fetchmail

    Payton Swick, Jan 5, 2006, in forum: Ruby
    Replies:
    7
    Views:
    119
    tony summerfelt
    Jan 11, 2006
  4. Rogue Amateur
    Replies:
    6
    Views:
    111
    Rogue Amateur
    Jul 14, 2006
  5. Michael Furmaniuk

    Should I use Daemonize or something else?

    Michael Furmaniuk, Mar 26, 2009, in forum: Ruby
    Replies:
    0
    Views:
    96
    Michael Furmaniuk
    Mar 26, 2009
Loading...

Share This Page