1uwfhw
Last Updated: January 25, 2017
·
9.972K
· artnez
4e482aa3f689e48f19c2055c25eee74b

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!

Say Thanks
Respond

6 Responses
Add your response

9929
9994a8cd682499af8cb46caea6df8153

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 ·
10810
1c716d07bb548234161d44b34615d730

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 ·
14106
Avatar

This was super helpful! Thanks!

over 1 year ago ·
20791
None

Thanks Artem! Totally helped me out.

over 1 year ago ·
20792
None

Thanks Artem! Totally helped me out.

over 1 year ago ·
28537

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"
11 months ago ·
Filed Under