- 安装 inotify-tools 插件(易可源码安装)
sudo apt-get update
sudo apt-get install inotify-tools
#或者
sudo yum install inotify-tools
- 编写测试脚本(如果脚本执行不成功,可能需要ssh-keygen 证书授权)
#!/bin/bash
# Get the current user
CURRENT_USER="$USER"
# Source and destination folders
SOURCE_FOLDER="/path/to/source/folder"
DESTINATION_HOST="username@destination_host"
DESTINATION_FOLDER="/path/to/destination/folder/$CURRENT_USER"
# Log file
LOG_FILE="/path/to/log/file.log"
# Function to sync files using rsync
sync_files() {
rsync -avz --delete "$SOURCE_FOLDER/" "$DESTINATION_HOST:$DESTINATION_FOLDER/"
}
# Function to log changes
log_change() {
echo "$(date): Change detected by $CURRENT_USER - $1 $2" >> "$LOG_FILE"
}
# Monitor changes using inotifywait
while true; do
inotifywait -r -e modify,create,delete,move "$SOURCE_FOLDER" |
while read path action file; do
log_change "$action" "$file"
sync_files
done
done
- 启动脚本
#脚本后台运行01
nohup ./your_script.sh > script_output.log 2>&1 &
#后台运行脚本02
npm install -g pm2
pm2 start your_script.sh --name your_script
pm2 logs your_script
- 查看运行时日志
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
sending incremental file list
./
text.txt
sent 110 bytes received 38 bytes 98.67 bytes/sec
total size is 0 speedup is 0.00
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
- 查看文件变更日志
2023年 08月 17日 星期四 10:06:01 CST: Change detected by root - CREATE text.txt