run_long_articles_job.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "run_sph_video_crawler|03:00|${LOG_DIR}/run_sph_video_crawler_$(date +'%Y-%m-%d').log"
  31. "run_piaoquan_video_crawler|06:00|${LOG_DIR}/run_piaoquan_video_crawler_$(date +'%Y-%m-%d').log"
  32. "run_sohu_video_crawler|06:10|${LOG_DIR}/run_sohu_video_crawler_$(date +'%Y-%m-%d').log"
  33. "top_article_generalize|11:20|${LOG_DIR}/top_article_generalize_$(date +'%Y-%m-%d').log"
  34. "run_sph_video_crawler|15:00|${LOG_DIR}/run_sph_video_crawler_$(date +'%Y-%m-%d').log"
  35. )
  36. # *************** 函数定义 ***************
  37. # 初始化环境
  38. initialize() {
  39. # 创建日志目录
  40. mkdir -p "${LOG_DIR}"
  41. # 设置当前时间变量
  42. current_time=$(date '+%Y-%m-%d %H:%M:%S')
  43. current_hour_minute=$(date '+%H:%M')
  44. current_weekday=$(date +%u) # 1=周一, 7=周日
  45. # 主日志文件(记录调度过程)
  46. MAIN_LOG="${LOG_DIR}/scheduler_$(date +'%Y-%m-%d').log"
  47. touch "${MAIN_LOG}"
  48. # 重定向所有输出到主日志
  49. exec >> "${MAIN_LOG}" 2>&1
  50. }
  51. # 启动任务函数
  52. start_task() {
  53. local task_name=$1
  54. local log_file=$2
  55. # 创建任务日志文件
  56. touch "${log_file}"
  57. # 检查进程是否已在运行
  58. if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
  59. echo "${current_time} - [INFO] 任务 ${task_name} 已在运行中" | tee -a "${log_file}"
  60. return 0
  61. fi
  62. # 切换到工作目录
  63. cd "${SCRIPT_DIR}" || {
  64. echo "${current_time} - [ERROR] 无法切换到目录 ${SCRIPT_DIR}" | tee -a "${log_file}"
  65. return 1
  66. }
  67. # 激活 Conda 环境
  68. if [[ -f "${CONDA_PATH}" ]]; then
  69. source "${CONDA_PATH}"
  70. conda activate "${CONDA_ENV}" || {
  71. echo "${current_time} - [ERROR] 无法激活 Conda 环境 ${CONDA_ENV}" | tee -a "${log_file}"
  72. return 1
  73. }
  74. else
  75. echo "${current_time} - [WARNING] Conda 初始化文件未找到: ${CONDA_PATH}" | tee -a "${log_file}"
  76. fi
  77. # 启动任务脚本
  78. echo "${current_time} - [INFO] 启动任务: ${task_name}" | tee -a "${log_file}"
  79. nohup python3 "${PYTHON_SCRIPT}" --task_name "${task_name}" >> "${log_file}" 2>&1 &
  80. # 检查是否启动成功
  81. sleep 1
  82. if pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}" > /dev/null; then
  83. local pid=$(pgrep -f "python3 ${PYTHON_SCRIPT} --task_name ${task_name}")
  84. echo "${current_time} - [SUCCESS] 任务启动成功: ${task_name} (PID: ${pid})" | tee -a "${log_file}"
  85. else
  86. echo "${current_time} - [ERROR] 任务启动失败: ${task_name}" | tee -a "${log_file}"
  87. fi
  88. }
  89. # 特殊日期检查函数
  90. is_special_day() {
  91. local task_name=$1
  92. local scheduled_time=$2
  93. # 示例:每周一执行的任务
  94. if [[ "${task_name}" == "weekly_report" ]]; then
  95. [[ "${current_weekday}" == "1" ]] # 周一
  96. return $?
  97. fi
  98. # 示例:每月1号执行的任务
  99. if [[ "${task_name}" == "monthly_report" ]]; then
  100. [[ "$(date +'%d')" == "01" ]]
  101. return $?
  102. fi
  103. # 默认每天都执行
  104. return 0
  105. }
  106. # 主调度函数
  107. schedule_tasks() {
  108. echo "====== ${current_time} 开始任务调度 ======"
  109. echo "当前时间: ${current_hour_minute}, 星期: ${current_weekday}"
  110. for task_config in "${TASKS[@]}"; do
  111. # 解析任务配置
  112. IFS='|' read -r task_name scheduled_time log_file <<< "${task_config}"
  113. # 检查是否到达执行时间
  114. if [[ "${current_hour_minute}" == "${scheduled_time}" ]]; then
  115. start_task "${task_name}" "${log_file}"
  116. else
  117. echo "${current_time} - [SCHEDULE] 未到执行时间: ${task_name} (计划: ${scheduled_time})" | tee -a "${log_file}"
  118. fi
  119. done
  120. echo "====== ${current_time} 任务调度完成 ======"
  121. echo ""
  122. }
  123. # *************** 主程序 ***************
  124. initialize
  125. schedule_tasks
  126. # 日志清理(保留最近7天日志)
  127. find "${LOG_DIR}" -name "*.log" -mtime +7 -delete