This is a Bash script designed to automatically execute PHP migration scripts within the Pagoda Panel scheduled tasks. Its primary function is to ensure that the migration script is run only once per instance, prevent duplicate executions, and log both the execution process and the results to a log file.
#!/bin/bash
# 自动迁移脚本 - 配合宝塔计划任务使用
# ===== 配置区域 =====
PHP_PATH="/www/server/php/83/bin/php" # PHP 可执行文件路径(根据实际版本修改)
SCRIPT_PATH="/www/wwwroot/1.php"
LOG_FILE="/www/server/cron/4041704b46a517bb6a6a28b638721a08.log"
PID_FILE="/www/wwwroot/migrate.pid"
# ====================
# 检查是否已在运行
if [ -f "$PID_FILE" ]; then
OLD_PID=$(cat "$PID_FILE")
if ps -p "$OLD_PID" > /dev/null 2>&1; then
echo "$(date '+%Y-%m-%d %H:%M:%S') 迁移脚本已在运行 (PID: $OLD_PID),跳过本次执行。" >> "$LOG_FILE"
exit 0
else
rm -f "$PID_FILE"
fi
fi
# 记录当前进程 PID
echo $$ > "$PID_FILE"
# 执行迁移脚本,输出重定向到日志文件
echo "$(date '+%Y-%m-%d %H:%M:%S') 开始执行迁移脚本..." >> "$LOG_FILE"
$PHP_PATH $SCRIPT_PATH >> "$LOG_FILE" 2>&1
# 执行完毕,清理 PID 文件
rm -f "$PID_FILE"
echo "$(date '+%Y-%m-%d %H:%M:%S') 迁移脚本执行完毕。" >> "$LOG_FILE"This script is typically used in the "Scheduled Tasks" section of the PaTa Panel to periodically (e.g., every minute or every hour) execute a PHP migration task. It prevents overlapping task executions by using a PID file (so that the next task does not run until the previous one has been completed) and logs all output to a specified log file, facilitating troubleshooting.