16

I have ubuntu virtual machine that I'm accessing through Vagrant. On my host (Mac OSX), there is a folder with couple of files, that I share to virtual machine. In that virtual machine, I want to use guard to watch for files changes and perform some action if any of those files changes.

I set up guard properly, and when changing shared file from within virtual machine, it works ok and runs appropriate scripts. But, if I try to change shared file from my host machine, this file change event doesn't propagate and guard doesn't react.

This is how my vagrant shared folder looks like (pretty usual stuff)

local_config.vm.share_folder "app", "/var/www/app/current", "../app"

I even tried using NFS sharing (:nfs => true) but it didn't help.

Is there any way to make file changes events propagate from host to virtual machine? Or is this something of Vagrant/VirtualBox nature?

UPDATE:

After some more trials, I installed ZenTest gem, which contains autotest tool allowing similar functionality regarding file change events.

When running autotest in virtual machine and changing files from my host machine, these changes are propagated and autotest reacts.

Based on this, it seems that the file change event propagation is an issue of guard, not vagrant or virtual box.

I haven't look into implementation differences between guard and autotest though.

Now I know it is possible to catch file change events from host in virtual machine. Does anyone have any idea on how to achieve this using guard? I like guard more because of its DSL and generic usability.

4 Answers 4

11

The root cause is that VirtualBox does not cascade the file change events on the host to the VM. Guard (on Linux) is expecting to receive notifications via inotify events. Instead, you can have guard poll for events with guard -p, but this can lead to your CPU maxing out, so you can throttle the polling back down with guard -p -l 10.

From guard help start:

  -l, [--latency=Overwrite Listen's default latency]
  -p, [--force-polling=Force usage of the Listen polling listener]

http://www.softr.li/blog/2012/07/21/running-guard-over-vagrant

1
  • Thanks Gabe, I left guard for watchr some time ago. Still, your answer is valuable for understanding.
    – rdamborsky
    Jun 7, 2013 at 6:41
4

I know this is an older question, but here is a more up-to-date answer:

Guard -o/--listen-on option documentation

Pasted here for quick reference:

-o/--listen-on option

Use Listen's network functionality to receive file change events from the
network. This is most useful for virtual machines (e.g. Vagrant) which have
problems firing native filesystem events on the guest OS.

Suggested use:

On the host OS, you need to listen to filesystem events and forward them to
your VM using the listen script:

    $ listen -f 127.0.0.1:4000

Remember to configure your VM to forward the appropriate ports, e.g.
in Vagrantfile:

    config.vm.network :forwarded_port, guest: 4000, host: 4000

Then, on your guest OS, listen to the network events but ensure you
specify the host path:

    $ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'
1

If anyone runs into this issue and guard is still not working...

I ended up using watchr. It is an alternative to guard. Events propagation from host to guest machine works ok in watchr. It is also more flexible than autotest.

1
  • Events propagation from host to guest machine works ok in watchr. How? Does it use polling? If VirtualBox is not propagating the events, how can it know when a file has changed?
    – Nateowami
    Jun 5, 2018 at 15:42
0

If you have this problem with hugo in Vagrant, you can start the server with a poll option:

hugo server --poll 2s

Docs:

--poll string            set this to a poll interval, e.g --poll 700ms, to use a 
                         poll based approach to watch for file system changes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .