|
@@ -0,0 +1,164 @@
|
|
|
+#!/bin/bash
|
|
|
+
|
|
|
+# =============================================================
|
|
|
+# 多任务定时调度管理器
|
|
|
+# 功能:在指定时间执行不同任务,每个任务有独立日志
|
|
|
+# 配置:在下方 "任务配置" 区域添加您的任务
|
|
|
+# 使用:设置cron每分钟执行此脚本: * * * * /path/to/this/script.sh
|
|
|
+# =============================================================
|
|
|
+
|
|
|
+# 确保脚本以root权限运行(按需修改)
|
|
|
+#if [ "$(id -u)" != "0" ]; then
|
|
|
+# echo "错误:此脚本需要以root权限运行" 1>&2
|
|
|
+# exit 1
|
|
|
+#fi
|
|
|
+
|
|
|
+# *************** 基础配置 ***************
|
|
|
+# 工作目录(脚本所在位置)
|
|
|
+SCRIPT_DIR="/root/luojunhui/LongArticlesJob"
|
|
|
+
|
|
|
+# 日志根目录
|
|
|
+LOG_DIR="${SCRIPT_DIR}/logs"
|
|
|
+
|
|
|
+# Conda 配置
|
|
|
+CONDA_PATH="/root/miniconda3/etc/profile.d/conda.sh"
|
|
|
+CONDA_ENV="tasks"
|
|
|
+
|
|
|
+# Python 脚本名称
|
|
|
+PYTHON_SCRIPT="long_articles_job.py"
|
|
|
+
|
|
|
+# *************** 任务配置 ***************
|
|
|
+# 格式: "任务名称|执行时间|日志文件路径"
|
|
|
+# 注意:
|
|
|
+# 1. 执行时间格式为 HH:MM (24小时制)
|
|
|
+# 2. 日志路径可使用变量 $(date +'格式')
|
|
|
+# 3. 添加新任务只需复制一行并修改参数
|
|
|
+TASKS=(
|
|
|
+ # 示例任务:每天09:00执行
|
|
|
+ "top_article_generalize|11:20|${LOG_DIR}/top_article_generalize_$(date +'%Y-%m-%d').log"
|
|
|
+
|
|
|
+# # 示例任务:每天12:30执行
|
|
|
+# "task2|12:30|${LOG_DIR}/task2_$(date +'%Y-%m-%d').log"
|
|
|
+#
|
|
|
+# # 示例任务:每天18:00执行
|
|
|
+# "task3|18:00|${LOG_DIR}/task3_$(date +'%Y-%m-%d').log"
|
|
|
+#
|
|
|
+# # 示例任务:每周一08:00执行
|
|
|
+# "weekly_report|08:00|${LOG_DIR}/weekly_$(date +'%Y-%m-%d').log"
|
|
|
+
|
|
|
+ # 在此添加您的自定义任务...
|
|
|
+ # "your_task_name|HH:MM|${LOG_DIR}/custom_$(date +'%Y-%m-%d').log"
|
|
|
+)
|
|
|
+
|
|
|
+# *************** 函数定义 ***************
|
|
|
+# 初始化环境
|
|
|
+initialize() {
|
|
|
+ # 创建日志目录
|
|
|
+ mkdir -p "${LOG_DIR}"
|
|
|
+
|
|
|
+ # 设置当前时间变量
|
|
|
+ current_time=$(date '+%Y-%m-%d %H:%M:%S')
|
|
|
+ current_hour_minute=$(date '+%H:%M')
|
|
|
+ current_weekday=$(date +%u) # 1=周一, 7=周日
|
|
|
+
|
|
|
+ # 主日志文件(记录调度过程)
|
|
|
+ MAIN_LOG="${LOG_DIR}/scheduler_$(date +'%Y-%m-%d').log"
|
|
|
+ touch "${MAIN_LOG}"
|
|
|
+
|
|
|
+ # 重定向所有输出到主日志
|
|
|
+ exec >> "${MAIN_LOG}" 2>&1
|
|
|
+}
|
|
|
+
|
|
|
+# 启动任务函数
|
|
|
+start_task() {
|
|
|
+ local task_name=$1
|
|
|
+ local log_file=$2
|
|
|
+
|
|
|
+ # 创建任务日志文件
|
|
|
+ touch "${log_file}"
|
|
|
+
|
|
|
+ # 检查进程是否已在运行
|
|
|
+ if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
|
|
|
+ echo "${current_time} - [INFO] 任务 ${task_name} 已在运行中" | tee -a "${log_file}"
|
|
|
+ return 0
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 切换到工作目录
|
|
|
+ cd "${SCRIPT_DIR}" || {
|
|
|
+ echo "${current_time} - [ERROR] 无法切换到目录 ${SCRIPT_DIR}" | tee -a "${log_file}"
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+
|
|
|
+ # 激活 Conda 环境
|
|
|
+ if [[ -f "${CONDA_PATH}" ]]; then
|
|
|
+ source "${CONDA_PATH}"
|
|
|
+ conda activate "${CONDA_ENV}" || {
|
|
|
+ echo "${current_time} - [ERROR] 无法激活 Conda 环境 ${CONDA_ENV}" | tee -a "${log_file}"
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+ else
|
|
|
+ echo "${current_time} - [WARNING] Conda 初始化文件未找到: ${CONDA_PATH}" | tee -a "${log_file}"
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 启动任务脚本
|
|
|
+ echo "${current_time} - [INFO] 启动任务: ${task_name}" | tee -a "${log_file}"
|
|
|
+ nohup python3 "${PYTHON_SCRIPT}" --task_name "${task_name}" >> "${log_file}" 2>&1 &
|
|
|
+
|
|
|
+ # 检查是否启动成功
|
|
|
+ sleep 1
|
|
|
+ if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
|
|
|
+ local pid=$(pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}")
|
|
|
+ echo "${current_time} - [SUCCESS] 任务启动成功: ${task_name} (PID: ${pid})" | tee -a "${log_file}"
|
|
|
+ else
|
|
|
+ echo "${current_time} - [ERROR] 任务启动失败: ${task_name}" | tee -a "${log_file}"
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
+# 特殊日期检查函数
|
|
|
+is_special_day() {
|
|
|
+ local task_name=$1
|
|
|
+ local scheduled_time=$2
|
|
|
+
|
|
|
+ # 示例:每周一执行的任务
|
|
|
+ if [[ "${task_name}" == "weekly_report" ]]; then
|
|
|
+ [[ "${current_weekday}" == "1" ]] # 周一
|
|
|
+ return $?
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 示例:每月1号执行的任务
|
|
|
+ if [[ "${task_name}" == "monthly_report" ]]; then
|
|
|
+ [[ "$(date +'%d')" == "01" ]]
|
|
|
+ return $?
|
|
|
+ fi
|
|
|
+
|
|
|
+ # 默认每天都执行
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+# 主调度函数
|
|
|
+schedule_tasks() {
|
|
|
+ echo "====== ${current_time} 开始任务调度 ======"
|
|
|
+ echo "当前时间: ${current_hour_minute}, 星期: ${current_weekday}"
|
|
|
+
|
|
|
+ for task_config in "${TASKS[@]}"; do
|
|
|
+ # 解析任务配置
|
|
|
+ IFS='|' read -r task_name scheduled_time log_file <<< "${task_config}"
|
|
|
+
|
|
|
+ # 检查是否到达执行时间
|
|
|
+ if [[ "${current_hour_minute}" == "${scheduled_time}" ]]; then
|
|
|
+ start_task "${task_name}" "${log_file}"
|
|
|
+ else
|
|
|
+ echo "${current_time} - [SCHEDULE] 未到执行时间: ${task_name} (计划: ${scheduled_time})" | tee -a "${log_file}"
|
|
|
+ fi
|
|
|
+ done
|
|
|
+
|
|
|
+ echo "====== ${current_time} 任务调度完成 ======"
|
|
|
+ echo ""
|
|
|
+}
|
|
|
+
|
|
|
+# *************** 主程序 ***************
|
|
|
+initialize
|
|
|
+schedule_tasks
|
|
|
+
|
|
|
+# 日志清理(保留最近7天日志)
|
|
|
+find "${LOG_DIR}" -name "*.log" -mtime +7 -delete
|