使用inotifywait监视多个目录并运行脚本
问题描述:
我有多个包含网站的git存储库。我想针对克隆的本地版本运行inotifywait以监视某些文件事件,并在检测到这些事件时自动运行git push和git pull脚本。使用inotifywait监视多个目录并运行脚本
到目前为止,我已经创建了每个目录的独立功能的脚本,但只有第一个函数被调用。
#!/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 [email protected] 'cd /path/to/web/root; git pull';
done;
}
dbg;
website2() {
same thing as above
}
website2;
website3() {
same thing as above
}
website3;
.......
website10() {
........
}
website10;
如何构建这一代码更高效,更重要,完全可操作,而无需创建和管理10个单独的脚本。 我真的很想将这个逻辑放在一个文件中,我希望这是一个模块,以实现更大的项目。
请批评我的追问下,我的语法,思维过程等等等等,所以,我可以改进。 谢谢。
答
如果你的逻辑是一样的,你可以使用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 [email protected] '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 &