Vagrant – Running Rails server in the background

Tags :

Let's say when you boot up or restart your virtual machine, you want to run the Rails server in the background for your Rails application.

It is pretty easy. First, let's create a function for the script block. Put following code on the top of the Vagrant file.

def start_rails_server
<<-SHELL
cd /vagrant
# start rails server as a daemon
# to kill: sudo kill `cat tmp/pids/server.pid`
rails s -d
SHELL
end

Now in the Vagrantfile configuration block, use the script function and set it to run always. 

Vagrant.configure("2") do |config|
...
config.vm.provision "shell", inline: start_rails_server, run: 'always'
...
end

Leave a Reply

Your email address will not be published. Required fields are marked *