使用inotifywait监视多个目录并运行脚本

时间:2012-12-04 09:24:32

标签: bash inotify inotifywait

我有多个包含网站的git存储库。我想对其克隆的本地版本运行inotifywait,以便监视某些文件事件,并在检测到这些事件时自动运行git push和git pull脚本。

到目前为止,我已经为每个目录创建了一个包含单独函数的脚本,但只调用了第一个函数。

  #!/usr/bin/env bash

  dbg() {
  inotifywait -mr -e ATTRIB /path/to/dbg/ |
  while read dir ev file;
  do
  cd /path/to/dbg
  git pull;
  git add .;
  git commit -m " something regarding this website has changed. check .... for more        info";
  git push;
  ssh remote@server.com 'cd /path/to/web/root; git pull';
  done;
  }
  dbg;

  website2() {
  same thing as above
  }
  website2;

  website3() {
  same thing as above
  }
  website3;

  .......

  website10() {
  ........
  }
  website10;

如何构建这一部分代码,使其更高效,更重要,更全面地运行,而无需创建和管理10个单独的脚本。 我真的希望将这个逻辑保存在一个文件中,我希望这是一个模块,以实现更大的项目。

请批评我的提问,语法,思维过程等等。这样我才能改进。 谢谢。

1 个答案:

答案 0 :(得分:2)

如果逻辑相同,则可以使用bash函数来避免复制。此外,提交消息也可以作为参数传递。 试试这个

#!/usr/bin/env bash

dbg() {
dbg_dir=$1
webroot_dir=$2
inotifywait -mr -e ATTRIB $dbg_dir |
while read dir ev file;
do
cd /path/to/dbg
git pull;
git add .;
git commit -m " something regarding this website has changed. check .... for more        info";
git push;
ssh remote@server.com 'cd $webroot_dir; git pull';
done;
}
dbg /path/to/dbg /path/to/webroot1 &  # & will run the command in background
dbg /path/to/dbg2 /path/to/webroot2 &
dbg /path/to/dbg3 /path/to/webroot3 &
相关问题