0

当在脚本中检测到配置更改时,我想使用 inotifywait 重新启动 nginx。问题是,如果我在守护进程模式下运行它,它会不断重启 nginx。

脚本如下所示:

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

注意:此脚本是 docker 入口点脚本的一部分。

4

2 回答 2

0

你可以试试下面的脚本。它会在执行此操作之前检查是否安装了 pyinotify。

 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()
于 2016-09-22T22:14:10.153 回答
0

当您inotifywait进入守护程序模式(-d选项)时,它会分叉到后台进程并返回。通过从while循环中调用它,您将创建许多inotifywait在后台运行的守护进程。

不要通过-d,它会起作用的。

于 2016-09-22T23:44:34.883 回答