Launching Multiple Processes with a Single Command in Rails

January 30, 2022   • rails

An application doesn’t exist in isolation. Usually, it depends on other processes to work correctly, such as a database, JavaScript / CSS compilers, Redis, etc.

So far, whenever I started the Rails app, I launched these supporting processes in multiple terminal windows. This week, I learned a new way to do this using a single command using the Foreman gem, which has saved me a lot of time.

Here’s a simple way to achieve this, inspired by the tailwindcss-rails gem.

Step 1: Create an executable dev file in the bin/ directory

cd bin
touch dev
chmod 755 bin/dev

Step 2: Add Foreman script to your bin/dev file

#!/usr/bin/env bash

if ! command -v foreman &> /dev/null
  echo "Installing foreman..."
  gem install foreman

foreman start -f

Step 3: Add the procfile

web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch
js: ..
mysql: ..
redis: ..

Now all you have to do is run the bin/dev command from the root of your rails application, and Foreman takes care of starting the processes listed in the Procfile.

I hope that helps.