Question

How do I run a bash command when Unicorn starts up on rails?

I’m currently working on an instance with a Rails server with Unicorn on Ubuntu. I’ve gotten everything running, except for one thing.

There is a gem I’m using called delayed jobs, and it needs to start as a daemon to function correctly whenever the server restarts. The problem is that it seems like the gem has very many different ways of starting, many of which aren’t supported with my current version. So, I thought instead of searching through piles of inapplicable docs, I could just start it up with a shell command that I know works.

I’m not using anything like Capistrano - so what would the best way of running a command-line script when Unicorn starts be? I have to run

cd /home/rails/ 
RAILS_ENV=production bin/delayed_job start

Thanks.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 28, 2016
Accepted Answer

Depending on how you are using and starting Unicorn, the most straightforward way to do this would probably be to use a Procfile along with a tool like Foreman. This allows you to define separate tasks to start. E.g.:

Procfile
web: unicorn -p $PORT -c ./config/unicorn.rb
worker: ./bin/delayed_job start

Then, running foreman start will launch both processes.

Foreman can also be used to generate a set of systemd units or Upstart confs based on the Procfile.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel