#!/bin/bash

# 获取当前日期，格式为 YYYY-MM-DD
CURRENT_DATE=$(date +%F)

# 日志文件路径，包含日期
LOG_FILE="/root/luojunhui/logs/update_published_articles_task_log_$CURRENT_DATE.txt"

# 重定向整个脚本的输出到带日期的日志文件
exec >> "$LOG_FILE" 2>&1
if pgrep -f "python3 updatePublishedMsgDaily.py" > /dev/null
then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - updatePublishedMsgDaily.py is running"
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') - trying to restart updatePublishedMsgDaily.py"
    cd /root/luojunhui/LongArticlesJob
    source /root/miniconda3/etc/profile.d/conda.sh
    conda activate tasks

    current_time=$(date +%H:%M)
    target_time="19:00"

    if [[ "$current_time" < "$target_time" ]]; then
        nohup python3 updatePublishedMsgDaily.py --run_task update >> "${LOG_FILE}" 2>&1 &
    else
        nohup python3 updatePublishedMsgDaily.py >> "${LOG_FILE}" 2>&1 &
    echo "$(date '+%Y-%m-%d %H:%M:%S') - successfully restarted updatePublishedMsgDaily.py"
    fi
fi