DEV Community 👩‍💻👨‍💻

Akshay Khot
Akshay Khot

Posted on • Originally published at akshaykhot.com

Launching Multiple Processes with a Single Command in 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
Enter fullscreen mode Exit fullscreen mode

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

#!/usr/bin/env bash

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

foreman start -f Procfile.dev
Enter fullscreen mode Exit fullscreen mode

Step 3: Add the procfile

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

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.

Top comments (2)

Collapse
 
aquadrehz profile image
AquaDrehz

Nice quickstart thanks!

Collapse
 
software_writer profile image
Akshay Khot Author

Thanks, glad it helped!

🌱 DEV runs on 100% open source code that we started called Forem.

 
You can contribute to the codebase or host your own.