How can I automatically restart my app after a crash?

There are a few different stopgaps you might consider implementing to automatically restart an app after it crashes.

  1. One lightweight method that is framework-agnostic is to just wrap your service definitions in a bash while loop. Your Procfile might look like:
1
  web: while true; do bundle exec rails server; sleep 1; done

or

1
  web: while true; do node app.js; sleep 1; done
  1. If you’re using Rails, you might consider a process monitoring tool like God to start your Rails server, instead of starting it directly. For example, if you’re running bundle exec rails server to launch your server, you could use the following god.rb (committed to the top level of your repo):
1
2
3
4
5
   God.watch do |w|
     w.name = 'rails'
     w.start = 'bundle exec rails server'
     w.keepalive
   end

Your Procfile would be:

1
  web: god -c god.rb -D
  1. A similar approach for Node apps is to use the forever package, and including the following line in your Procfile:
1
  web: forever app.js

The Enclave Platform