Question

I'd like to run a ruby mongrel script as the last step of my provisioning system (CHEF). Therefore I wrote an upstart .conf file with the following entries:

#!upstart
description "mongrel server"
author      "daniele"

start on startup
stop on shutdown

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
expect fork

# Run before process
pre-start script
end script

# Start the process
script
   cd /vagrant/trunk
   /bin/sh /vagrant/trunk/script/server -p 3000 >> /home/vagrant/log.txt
end script

However the log.txt file empty and running netstat -an | grep 3000 shows nothing. I thought that the script wasn't executable, but running chmod didn't change anything.

However, if I execute the script manually, I can start the server

vagrant@ubuntu10:/vagrant/trunk$ ./script/server 
=> Booting Mongrel
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
/usr/local/rvm/gems/ruby-1.8.7-p352/gems/rails-2.3.4/lib/rails/gem_dependency.rb:119:Warning:Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010.  Use #requirement
=> Call with -d to detach
=> Ctrl-C to shutdown server

the content of the script is:

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/server'

I run on Vagrant with RVM and Ruby 1.8.7, Rubygems 1.3.7. The bootstrap recipe is:

[...]
template "mongrel.upstart.conf" do
   path "/etc/init/mongrel.conf"
   source "mongrel.upstart.conf.erb"
   mode 0644
   owner "root"
   group "root"
end

service "mongrel" do
   provider Chef::Provider::Service::Upstart
   supports :restart => true, :start => true, :stop => true
   action [:enable, :start]
end

Any idea? Thanks

Was it helpful?

Solution

The problem is that your Upstart config runs before Vagrant has finished setting up the shared folder where you code is mounted. When you run it manually from the commandline later it has already been mounted.

I'm not sure what the solution is -- if you could hook in to the boot process where this takes place, you could emit an event, such as:

initctl emit vagrant-mounted

that your Upstart config could wait on

start on vagrant-mounted

OTHER TIPS

I sort of solved a similar problem by adding

sleep 10

to the script. Like this:

...
# Start the process
script
    sleep 10
    cd /vagrant/trunk
    /bin/sh /vagrant/trunk/script/server -p 3000 >> /home/vagrant/log.txt
end script
....

At least in Ubuntu 12.04 you can wait for the "mounted" signal which is emited by the mountall job.

start on mounted

The above stanza should work with Vagrant shared folders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top