1

I have created a program that I need to run constantly. It currently lives at scripts/mailman. I start it by doing this:

sudo bundle exec rails runner script/mailman &

It seams to stop after I logout of the server. Here is the contents of my mailman program:

#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"
require "rb-inotify"

Mailman.config.logger = Logger.new("/var/log/mailman.log")
Mailman.config.maildir = '/var/mail'
require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!
Mailman::Application.run do
   default do
       begin
           Bin.receive_mail(message)
       end
   end
end

What is a good way to start this program automatically and keep it running always? I am running this on Ubuntu.

user985723
  • 628
  • 2
  • 8
  • 18
  • Did you find a solid answer? We also have issues keeping our mailman server running and I'm considering tying it into Resque. – Tass Nov 19 '14 at 19:24

2 Answers2

2

I've found that the daemons gem works well for this.

Assuming your posted code lives in script/mailman.rb, you can make a file script/mailman_ctl:

#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
Daemons.run('mailman.rb')

I typically give the options {:backtrace => true, :monitor => true} to the Daemons.run call, so that I have a better idea of what happened if the process ever dies.

  • Ok. I've done that and did: `sudo ruby script/mailman_ctl start` but I do not see it running using ps or the log file isn't populating itself. – user985723 Mar 14 '13 at 05:38
2

Use the 'daemons' gem as suggested here:
Make a Ruby program a daemon?

Seems also to be very popular on RubyToolbox
https://www.ruby-toolbox.com/categories/daemonizing

Community
  • 1
  • 1
deepflame
  • 928
  • 1
  • 8
  • 19