使用screen和debounce同步cosmd

使用debounce延迟间断性同步

/usr/local/bin/sync-oss.sh写入如下代码

#!/bin/bash

LOGFILE="/var/log/sync-oss.log"

debounce() {
local interval=$1
local source=$2
shift 2
local cmd=("$@")

local last_run=0

while true; do
# Read from input (inotifywait)
read inotify_output

# Log the output of inotifywait
echo "[$(date)] Inotify detected: $inotify_output from directory: $source" >> $LOGFILE

# 当前时间戳
local now=$(date +%s)

# 如果时间间隔小于设定的间隔,则跳过此次同步
if (( now - last_run < interval )); then
echo "[$(date)] Change detected, but skipping sync due to debounce interval." >> $LOGFILE
continue
fi

# 更新上次同步的时间戳
last_run=$now

echo "[$(date)] Executing sync command for directory: $source." >> $LOGFILE
"${cmd[@]}"
if [ $? -eq 0 ]; then
echo "[$(date)] Sync command executed successfully for directory: $source." >> $LOGFILE
else
echo "[$(date)] Sync command failed for directory: $source." >> $LOGFILE
fi
done
}

watch_and_sync() {
local interval=$1
local source=$2
local target=$3

# Check if source directory exists
if [ ! -d "$source" ]; then
echo "[$(date)] Directory $source does not exist. Skipping sync." >> $LOGFILE
return 1
fi

echo "[$(date)] Watching and syncing $source to $target with interval: $interval seconds" >> $LOGFILE
inotifywait -m -r -e create,delete,modify,move --format '%w%f' "$source" | debounce "$interval" "$source" coscmd upload -rs --delete --yes "$source" "$target"
}

# Setting up different intervals for directories
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2024/11" "/wp-content/uploads/2024/11" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2024/12" "/wp-content/uploads/2024/12" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2025/01" "/wp-content/uploads/2025/01" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2025/02" "/wp-content/uploads/2025/02" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2025/03" "/wp-content/uploads/2025/03" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2025/04" "/wp-content/uploads/2025/04" &
watch_and_sync 60 "/www/wwwroot/jre-li.com/wp-content/uploads/2025/05" "/wp-content/uploads/2025/05" &
watch_and_sync 3600 "/www/wwwroot/jre-li.com/wp-content/themes/jreinfo/assets" "/wp-content/themes/jreinfo/assets" &

echo "[$(date)] sync-oss.sh script started." >> $LOGFILE

安装inotifywait

sudo apt-get update
sudo apt-get install inotify-tools -y

设置screen

Debian安装screen

apt update

apt install screen

启动新的 screen 会话

screen -S syncOssSession

这里的 -S 参数允许您为 screen 会话命名(即syncOssSession),这样您就可以稍后更轻松地重新连接到它。

在新的 screen 会话中,运行您的脚本:

/usr/local/bin/sync-oss.sh

如果您想让脚本在后台运行并从 screen 会话中断开连接,可以按 Ctrl + A,然后按 D。这将“分离” screen 会话,但您的脚本仍然在后台运行。

要重新连接到 screen 会话

(例如,要检查脚本的输出或停止脚本),使用以下命令:

screen -r syncOssSession

如果您完成了并想退出 screen 会话,首先确保停止了您的脚本,然后输入 exit

关闭某个screen

首先,使用以下命令确定会话的名称或ID:

screen -ls

基于会话ID或名称,使用以下命令来关闭该会话:

screen -X -S [session_name_or_ID] quit

例如,要关闭ID为12345的会话,您可以使用:

screen -X -S 12345 quit

如果您给会话命名为syncOssSession,则可以使用:

screen -X -S syncOssSession quit

关闭全部screen

screen -ls | grep Detached | awk '{print $1}' | xargs -I {} screen -X -S {} quit

脚本相关

检查脚本是否在运行

ps aux | grep sync-oss.sh

要终止名为sync-oss.sh的所有进程

可以使用:

pkill -f sync-oss.sh

这里的-f选项意味着全命令行模式,这样它会匹配整个命令行而不仅仅是进程名,这在您的脚本或其他具有完整路径名的进程上特别有用。

和之前一样,如果您需要强制终止进程,您可以与-9信号结合使用:

pkill -f -9 sync-oss.sh