Search code examples
ubuntuvagrantsystemd

How to run nginx.service after /vagrant is mounted


What I want to do?

I'm trying to make nginx load configurations from /vagrant mounted by vagrant automatically.

So I edited nginx.service to make it boot after shared folder mounted, but it not works.

Certainly nginx has been booted after virtualbox-guest-utils.service, however, it seems to be booted before vagrant.mount (/vagrant). Because nginx couldn't load configurations from /vagrant and it works after running command systemctl restart nginx.service manually.

How to run .service unit after auto generated .mount unit started?

Environment

  • Vagrant 1.8.1
  • Ubuntu Server 15.10 (ubuntu/wily64)
  • VirtualBox 5.0.14

systemctl cat nginx.service

Pattern 1 : Not work

# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

# /etc/systemd/system/nginx.service.d/override.conf
[Unit]
Requires=virtualbox-guest-utils.service vagrant.mount
After=virtualbox-guest-utils.service vagrant.mount

Pattern 2 : Not work

# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

# /etc/systemd/system/nginx.service.d/override.conf
[Unit]
RequiresMountsFor=/vagrant

Solution

  • I wrote a post how I'm using udev event to restart nginx and php5-fpm.

    In short I'm adding /etc/udev/rules.d/50-vagrant-mount.rules with rule:

    SUBSYSTEM=="bdi",ACTION=="add",RUN+="/bin/bash /root/.udev-mount-restart-services.sh"
    

    And the script /root/.udev-mount-restart-services.sh is:

    sleep 5 # wait for a bit for NFS to make sure resources are available
    systemctl restart php5-fpm > /dev/null 2>&1
    systemctl restart nginx > /dev/null 2>&1