On a recent project I had a chance to work with bluepill, a Ruby gem for simple process monitoring. For my own reference and also to hopefully help others, I decided to post a sample bluepill daemonizing project. This tutorial is designed to help you play with bluepill on a mac, so instructions here will differ from the *nix machine installation instructions found in the bluepill documentation.

Getting Started

These instructions assume that you already have a compatible version of Ruby.

  1. Install the bluepill gem. sudo gem install bluepill

  2. I found the bluepill logs very helpful for debugging, so carefully think through how you want to log. There are two concerns when setting up logging: log location and log rotation. You’ll want to make sure that you rotate your logs so that you don’t accidentally fill up your hard drive. Log rotation is managed by /etc/newsyslog on a mac and /etc/logrotate on other flavors of *nix machines. For help setting up log rotation on your mac, see the newsyslog man page.

  3. Lastly, create the /var/run/bluepill directory for bluepill to store its pid and sock files. Not sure what a pid file is? See this handy pid file definition. Bluepill will need to know your program’s pid so that it can stop, start, and restart your program.

Writing the Process to Daemonize

For my project, I wanted to daemonize a program that would check the number of items in a queue every five minutes. The sample Ruby code below has some of the same features of that program, most importantly, in both cases the program should not terminate.

while true
  puts "helloworld"

My sample program is not designed to terminate, but it still could terminate if a problem is encountered. This is where bluepill comes in. Bluepill will monitor the process and automatically restart it if it stops. Bluepill is an easy-to-use utility with many features beyond daemonizing a process, so do check out the bluepill documentation which includes other sample pill files. From here on, I’m going on to break down each part of this sample daemonizing pill file.

Bluepill.application("sample_daemon") do |app|
  app.process("test") do |process|
    process.start_command = "ruby path_to_helloworld.rb"
    process.pid_file = "/tmp/test.pid"
    process.stdout = process.stderr = "/tmp/server.log"
    process.daemonize = true

A pill file organizes processes into groups under an application. In this case, the application is “sample_daemon” and the process is “test.” When you manually stop/start/restart your daemon, you will call it by the process name. The process options that are set in this sample pill file are the minimal configuration needed for a daemonizing pill. You’ll need to know the location of your process’s pid file and your logs, and the how to start your process.

Run It

  1. Load the pill file. Pro tip: if you are using rvm to manage your gems, you’ll need to use rvmsudo here.sudo bluepill load path_to_test.pill

  2. You can now stop, start, restart, and check status of your pill file.sudo bluepill stop/start/restart/status process_name