0

I want to use inotifywait to restart nginx when configuration changes are detected in a script. The problem is that if I run it in daemon mode it keep restarting the nginx.

The script looks like this:

while inotifywait -d -o /var/log/bootstrap.log --format '%T %:e %w' --timefmt '%Y.%m.%d %H:%M:%S' -e modify,create,delete,move,attrib $(find -L /etc/nginx -type f)
do
  NGX_STATUS=$(nginx -t 2>&1)
  NGX_CFG_STATUS=$(echo $NGX_STATUS | grep successful)
  if [[ $(echo $?) == 0 ]]; then
      /etc/init.d/nginx restart
  else
    echo $NGX_STATUS | tee -a /var/log/bootstrap.log
  fi
done

Note: This script is part of the docker entrypoint script.

zozo6015
  • 557
  • 2
  • 11
  • 27

2 Answers2

0

You can try the script below. It checks to see if pyinotify is installed before doing it's thing.

 import sys
 import pip
 def install(package):
    pip.main(['install', package])
 try:
    import pyinotify
 except ImportError:
    print 'pyinotify is not installed, installing it now!'
    install('pyinotify')
 finally:
   import pyinotify,subprocess
   def onChange(ev):
     cmd = ['/bin/systemctl', 'reload', 'nginx.service']
     subprocess.Popen(cmd).communicate()
   wm = pyinotify.WatchManager()
   wm.add_watch('/etc/nginx/nginx.conf', pyinotify.IN_MODIFY,    onChange)
   notifier = pyinotify.Notifier(wm)
   notifier.loop()
jebjeb
  • 115
  • 1
  • 4
  • 12
0

When you put inotifywait in daemon mode (the -d option), it forks to a background process and returns. By calling it from a while loop, you are creating many inotifywait daemons which are running in the background.

Don't pass -d and it will work.

Tim
  • 4,790
  • 4
  • 33
  • 41
  • Any idea how to trigger the script with the -d option? Thing is that without the -d the docker will not start the script. – zozo6015 Sep 23 '16 at 00:03
  • You want to run the whole script in the background, not just `inotifywait`. I don't know how to do that with docker. That would be a good separate question. – Tim Sep 23 '16 at 00:19