#!/bin/bash

path=$1     # 爬虫路径
log_type=$2 # 爬虫策略
crawler=$3  # 哪款爬虫
env=$4      # 环境

if [ ${env} = "dev" ];then
  piaoquan_crawler_dir=/Users/wangkun/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
  log_path=${piaoquan_crawler_dir}monitor/logs/$(date +%Y-%m-%d)-shell.log
else
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
  log_path=${piaoquan_crawler_dir}monitor/logs/$(date +%Y-%m-%d)-shell.log
fi

echo "开始..." >> ${log_path}

# ====================进程心跳检测====================
echo "$(date "+%Y-%m-%d %H:%M:%S") 正在监测 run_cpu_memory 进程状态" >> ${log_path}
# 使用pgrep命令匹配进程名称为run_cpu_memory.py的进程数量
process_count=$(pgrep -f "run_cpu_memory.py" | wc -l)
if [[ "$process_count" -gt 0 ]]; then
  echo "$(date "+%Y-%m-%d %H:%M:%S") run_cpu_memory 进程状态正常。" >> ${log_path}
else
  echo "$(date "+%Y-%m-%d_%H:%M:%S") 未运行, 正在启动..." >> ${log_path}
  cd ${piaoquan_crawler_dir} && nohup ${python} -u ${path} --log_type="${log_type}" --crawler="${crawler}" --env="${env}" >> ${log_path}  &
  echo "$(date "+%Y-%m-%d %H:%M:%S") 启动完成!" >> ${log_path}
fi
# ===================================================

# 删除日志
echo "$(date "+%Y-%m-%d %H:%M:%S") 开始清理 10 天前的日志文件" >> ${log_path}
find ${piaoquan_crawler_dir}monitor/logs/ -mtime +10 -name "*.log" -exec rm -rf {} \;
echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件清理完毕" >> ${log_path}
exit 0