Skip to content

Instantly share code, notes, and snippets.

@wujiyu115
Created December 12, 2025 06:35
Show Gist options
  • Select an option

  • Save wujiyu115/a8335aef2d5c560d7da656e45366845d to your computer and use it in GitHub Desktop.

Select an option

Save wujiyu115/a8335aef2d5c560d7da656e45366845d to your computer and use it in GitHub Desktop.
icloudpd monitor
#!/bin/bash
# 配置变量
# 设置要 curl 的 URL
CURL_URL="https://xxxx/xxx"
# 设置要监控的目录路径(可配置)
MONITOR_DIR="/mnt/disk1/all_photo/iphone14"
# icloudpd 配置
ICLOUD_USERNAME="[email protected]"
ICLOUD_PASSWORD="xxx"
# 监控配置
SLEEP_INTERVAL=30 # 检查间隔(秒)
NO_CHANGE_THRESHOLD=25 # 连续无变化阈值
# 函数:获取文件数量
get_file_count() {
find "$MONITOR_DIR" -not -path '*/.*' -type f | wc -l
}
# 初始化计数器和上一次的文件数
counter=0
last_count=$(get_file_count)
# 初始化文件变化通知相关变量
change_counter=0
change_notification_interval=120 # 1小时 = 3600秒 / 30秒 = 120次检查
# 函数:启动 icloudpd 进程
start_icloudpd() {
echo "Starting icloudpd process..."
nohup ./icloudpd -u "$ICLOUD_USERNAME" -p "$ICLOUD_PASSWORD" -d iphone14 --domain cn > icloudpd.log 2>&1 &
echo "icloudpd process started."
}
# 函数:检查并重启 icloudpd
check_and_restart_icloudpd() {
if pgrep -f "icloudpd" > /dev/null; then
echo "icloudpd process is running."
else
echo "icloudpd process not found. Restarting..."
start_icloudpd
fi
}
# 主函数:监控文件变化
monitor_files() {
while true; do
# 等待指定间隔
sleep $SLEEP_INTERVAL
# 获取当前文件数
current_count=$(get_file_count)
# 比较文件数
if [ "$current_count" -eq "$last_count" ]; then
# 如果文件数没有变化,增加计数器
((counter++))
echo "No change in file count. Counter: $counter"
# 文件数没变化时,change_counter 也要增加
((change_counter++))
else
# 如果文件数变化了,重置无变化计数器
counter=0
((change_counter++))
echo "File count changed. New count: $current_count, Change counter: $change_counter"
fi
# 更新 last_count
last_count=$current_count
# 如果连续指定次数没有变化,执行 curl
if [ $counter -eq $NO_CHANGE_THRESHOLD ]; then
echo "No changes for $NO_CHANGE_THRESHOLD consecutive checks. Sending notification..."
curl -X GET \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Accept-Language: en-US,en;q=0.5" \
-H "Connection: keep-alive" \
-s "${CURL_URL}/文件监控通知/连续${NO_CHANGE_THRESHOLD}次检查无文件变化"
# 重置计数器
counter=0
# 检查并重启 icloudpd
check_and_restart_icloudpd
fi
# 如果达到 1 小时(120 次检查),无论是否有变化都发送通知
if [ $change_counter -eq $change_notification_interval ]; then
echo "1 hour passed. Sending periodic notification..."
curl -X GET \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
-H "Accept-Language: en-US,en;q=0.5" \
-H "Connection: keep-alive" \
-s "${CURL_URL}/文件监控通知/1小时定时通知-当前文件数${current_count}"
# 重置变化计数器
change_counter=0
fi
done
}
# 主程序逻辑
echo "Starting combined monitor script..."
# 启动 icloudpd 进程
start_icloudpd
# 开始监控文件变化
monitor_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment