Last Updated: February 25, 2016
·
3.337K
· jeroenj

Daemonize puma with launchd on Mac OSX with RVM

First set up your application in an rvm gemset:

rvm 1.9.3@my_app --create

Make sure you have puma installed in that gemset. You may also want to add it to your Gemfile, but that step is not required:

gem install puma

or

gem 'puma'

in your Gemfile.

Next you'll have to create an rvm wrapper for your puma executable so the correct gemset gets loaded from launchd:

rvm wrapper 1.9.3@my_app my_app puma

This will generate a myapppuma executable in /Users/your_username/.rvm/bin/expenses_puma.

Now you have to set up your Launch Agent in ~/Library/LaunchAgents/com.aname.myapp.plist. You have to add the following to that file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.a_name.my_app</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/your_username/.rvm/bin/my_app_puma</string>
    <string>-p9999</string>
    <string>/Users/your_username/apps/my_app/config.ru</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardErrorPath</key>
  <string>log/error.log</string>
  <key>StandardOutPath</key>
  <string>log/access.log</string>
  <key>WorkingDirectory</key>
  <string>/Users/your_username/apps/my_app</string>
</dict>
</plist>

Now you'll have to load your Launch Agent by running the following command in your Terminal:

launchctl load -w ~/Library/LaunchAgents/com.jeroen.expenses.plist

You may set a port or pass any other configuration option in the ProgramArguments array.