user985723
user985723

Reputation: 628

Turn a ruby script into an always running job

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.

Upvotes: 1

Views: 3274

Answers (2)

user684934
user684934

Reputation:

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.

Upvotes: 2

deepflame
deepflame

Reputation: 928

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

Upvotes: 2

Related Questions