如何使用 lsyncd 实时同步并执行 shell 命令

修改 lsyncd 的默认配置,不直接执行rsync 进行同步,而是改用自己的脚本。
binary 指定我们的脚本

vim /usr/local/lsyncd/etc/lsyncd.conf

settings  {
logfile = "/usr/local/lsyncd/logs/lsyncd.log",
statusFile = "/usr/local/lsyncd/logs/lsyncd.status",
maxDelays = 100,
statusInterval = 5,
delay = 5,
exitcodes = {[0] = "ok", [1] = "again", [2] = "die"} }
sync {
default.rsync,
source = "/data/logs/nginx",
target = "sync_user@192.168.31.140::sync_test",
exclude = { ".*", "*.tmp", "*.bak" , "*.pid"},
delete = true,
rsync = {
binary = "/data/sh/mysync.sh",
compress = false,
archive = true,
verbose = false,
timeout = 3600,
password_file = "/usr/local/lsyncd/etc/rsync.pass"
}
}
编写自己的脚本

vim /data/sh/mysync.sh

#!/bin/bash
/usr/bin/rsync "$@"
result=$?
args="$@"
(
if [ $result -eq 0 ]; then
echo "argument:$args" >> /tmp/lsync_test.txt
fi
) >/dev/null 2>/dev/null </dev/null exit $result
lsyncd 会调用 /data/sh/mysync.sh 脚本触发 rsync 同步,并执行脚本后续的命令。
上一篇:ServiceManager 小结


下一篇:android sdk manager 代理设置