run_long_articles_job.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. # =============================================================
  3. # 多任务定时调度管理器
  4. # 功能:在指定时间执行不同任务,每个任务有独立日志
  5. # 配置:在下方 "任务配置" 区域添加您的任务
  6. # 使用:设置cron每分钟执行此脚本: * * * * /path/to/this/script.sh
  7. # =============================================================
  8. # 确保脚本以root权限运行(按需修改)
  9. #if [ "$(id -u)" != "0" ]; then
  10. # echo "错误:此脚本需要以root权限运行" 1>&2
  11. # exit 1
  12. #fi
  13. # *************** 基础配置 ***************
  14. # 工作目录(脚本所在位置)
  15. SCRIPT_DIR="/root/luojunhui/LongArticlesJob"
  16. # 日志根目录
  17. LOG_DIR="${SCRIPT_DIR}/logs"
  18. # Conda 配置
  19. CONDA_PATH="/root/miniconda3/etc/profile.d/conda.sh"
  20. CONDA_ENV="tasks"
  21. # Python 脚本名称
  22. PYTHON_SCRIPT="long_articles_job.py"
  23. # *************** 任务配置 ***************
  24. # 格式: "任务名称|执行时间|日志文件路径"
  25. # 注意:
  26. # 1. 执行时间格式为 HH:MM (24小时制)
  27. # 2. 日志路径可使用变量 $(date +'格式')
  28. # 3. 添加新任务只需复制一行并修改参数
  29. TASKS=(
  30. # 示例任务:每天09:00执行
  31. "top_article_generalize|11:20|${LOG_DIR}/top_article_generalize_$(date +'%Y-%m-%d').log"
  32. # # 示例任务:每天12:30执行
  33. # "task2|12:30|${LOG_DIR}/task2_$(date +'%Y-%m-%d').log"
  34. #
  35. # # 示例任务:每天18:00执行
  36. # "task3|18:00|${LOG_DIR}/task3_$(date +'%Y-%m-%d').log"
  37. #
  38. # # 示例任务:每周一08:00执行
  39. # "weekly_report|08:00|${LOG_DIR}/weekly_$(date +'%Y-%m-%d').log"
  40. # 在此添加您的自定义任务...
  41. # "your_task_name|HH:MM|${LOG_DIR}/custom_$(date +'%Y-%m-%d').log"
  42. )
  43. # *************** 函数定义 ***************
  44. # 初始化环境
  45. initialize() {
  46. # 创建日志目录
  47. mkdir -p "${LOG_DIR}"
  48. # 设置当前时间变量
  49. current_time=$(date '+%Y-%m-%d %H:%M:%S')
  50. current_hour_minute=$(date '+%H:%M')
  51. current_weekday=$(date +%u) # 1=周一, 7=周日
  52. # 主日志文件(记录调度过程)
  53. MAIN_LOG="${LOG_DIR}/scheduler_$(date +'%Y-%m-%d').log"
  54. touch "${MAIN_LOG}"
  55. # 重定向所有输出到主日志
  56. exec >> "${MAIN_LOG}" 2>&1
  57. }
  58. # 启动任务函数
  59. start_task() {
  60. local task_name=$1
  61. local log_file=$2
  62. # 创建任务日志文件
  63. touch "${log_file}"
  64. # 检查进程是否已在运行
  65. if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
  66. echo "${current_time} - [INFO] 任务 ${task_name} 已在运行中" | tee -a "${log_file}"
  67. return 0
  68. fi
  69. # 切换到工作目录
  70. cd "${SCRIPT_DIR}" || {
  71. echo "${current_time} - [ERROR] 无法切换到目录 ${SCRIPT_DIR}" | tee -a "${log_file}"
  72. return 1
  73. }
  74. # 激活 Conda 环境
  75. if [[ -f "${CONDA_PATH}" ]]; then
  76. source "${CONDA_PATH}"
  77. conda activate "${CONDA_ENV}" || {
  78. echo "${current_time} - [ERROR] 无法激活 Conda 环境 ${CONDA_ENV}" | tee -a "${log_file}"
  79. return 1
  80. }
  81. else
  82. echo "${current_time} - [WARNING] Conda 初始化文件未找到: ${CONDA_PATH}" | tee -a "${log_file}"
  83. fi
  84. # 启动任务脚本
  85. echo "${current_time} - [INFO] 启动任务: ${task_name}" | tee -a "${log_file}"
  86. nohup python3 "${PYTHON_SCRIPT}" --task_name "${task_name}" >> "${log_file}" 2>&1 &
  87. # 检查是否启动成功
  88. sleep 1
  89. if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
  90. local pid=$(pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}")
  91. echo "${current_time} - [SUCCESS] 任务启动成功: ${task_name} (PID: ${pid})" | tee -a "${log_file}"
  92. else
  93. echo "${current_time} - [ERROR] 任务启动失败: ${task_name}" | tee -a "${log_file}"
  94. fi
  95. }
  96. # 特殊日期检查函数
  97. is_special_day() {
  98. local task_name=$1
  99. local scheduled_time=$2
  100. # 示例:每周一执行的任务
  101. if [[ "${task_name}" == "weekly_report" ]]; then
  102. [[ "${current_weekday}" == "1" ]] # 周一
  103. return $?
  104. fi
  105. # 示例:每月1号执行的任务
  106. if [[ "${task_name}" == "monthly_report" ]]; then
  107. [[ "$(date +'%d')" == "01" ]]
  108. return $?
  109. fi
  110. # 默认每天都执行
  111. return 0
  112. }
  113. # 主调度函数
  114. schedule_tasks() {
  115. echo "====== ${current_time} 开始任务调度 ======"
  116. echo "当前时间: ${current_hour_minute}, 星期: ${current_weekday}"
  117. for task_config in "${TASKS[@]}"; do
  118. # 解析任务配置
  119. IFS='|' read -r task_name scheduled_time log_file <<< "${task_config}"
  120. # 检查是否到达执行时间
  121. if [[ "${current_hour_minute}" == "${scheduled_time}" ]]; then
  122. start_task "${task_name}" "${log_file}"
  123. else
  124. echo "${current_time} - [SCHEDULE] 未到执行时间: ${task_name} (计划: ${scheduled_time})" | tee -a "${log_file}"
  125. fi
  126. done
  127. echo "====== ${current_time} 任务调度完成 ======"
  128. echo ""
  129. }
  130. # *************** 主程序 ***************
  131. initialize
  132. schedule_tasks
  133. # 日志清理(保留最近7天日志)
  134. find "${LOG_DIR}" -name "*.log" -mtime +7 -delete