Last Updated: October 16, 2018
·
10.62K
· artnez

Launch upstart services after Vagrant mount

When Vagrant boots, one of the last things it does is mount /vagrant/. This can be a problem if you're launching an upstart service that depends on the existence of /vagrant/ (a web server, for example).

One solution is to emit an upstart event in a Vagrantfile provisioning script. Then update your upstart config to start on that event. For example, in your upstart file do something like this:

description "my super upstart service"
start on vagrant-ready

... more upstart config ...

Then, in your Vagrantfile:

config.vm.provision :shell, :inline => "sudo initctl emit vagrant-ready"

And that's it!

5 Responses
Add your response

This article helped me indeed!

Polishing up, I found that there's actually an upstart event Vagrant emits each time it mounts a shared folder - called 'vagrant-mounted', along with an environment variable 'MOUNTPOINT=[path]' (see Upstart EventStructure, look up for the 'Environment' section ).

It's possible to have an upstart config file listening to the 'vagrant-mounted' event, and check for the $MOUNTPOINT value... mine went something like this:

start on vagrant-mounted
# ....
pre-start script
  [ $MOUNTPOINT = /vagrant ] || stop
end script
# The rest of the upstart config file....

This will eliminate the need of adding the suggested line to the Vagrantfile, and stick to what's already provided by Vagrant.

Hope this helps!

over 1 year ago ·

If you are not using Ubuntu and don't have upstart services you can also hook up to udev events http://razius.com/2013/11/launching-services-after-vagrant-mount/

over 1 year ago ·

Thanks Artem! Totally helped me out.

over 1 year ago ·

Thanks Artem! Totally helped me out.

over 1 year ago ·

On my vagrant 2.5.4 I get this error doing as suggested on the post:


==> default: Running provisioner: shell...
default: Running: inline script
==> default: stdin: is not a tty
==> default: initctl: Event failed
</pre>

When checking the vagrant doc I notice the syntax doesn't look correct.
This should be ok following vagrant doc:

config.vm.provision "shell", inline: "sudo initctl emit vagrant-ready"
over 1 year ago ·