crypto.ai/schedule_task.sh
2025-04-28 14:53:05 +08:00

137 lines
4.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 加密货币分析智能体定时任务启动脚本
# 该脚本用于在后台启动智能体定时任务
# 使用说明:
# ./schedule_task.sh start crypto 360 # 以360分钟为间隔启动加密货币智能体
# ./schedule_task.sh start gold "08:30" # 每天08:30运行黄金智能体
# ./schedule_task.sh start both 240 # 以240分钟为间隔同时启动两种智能体
# ./schedule_task.sh stop # 停止所有运行中的定时任务
# ./schedule_task.sh status # 查看运行状态
# 获取当前脚本所在目录
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
LOG_DIR="${SCRIPT_DIR}/logs"
SCHEDULE_LOG="${LOG_DIR}/schedule_task.log"
# 确保日志目录存在
mkdir -p "${LOG_DIR}"
# 激活虚拟环境(如果使用了虚拟环境)
VENV_PATH="${SCRIPT_DIR}/venv"
if [ -d "${VENV_PATH}" ]; then
source "${VENV_PATH}/bin/activate"
echo "已激活虚拟环境: ${VENV_PATH}"
fi
# 定义启动定时任务的函数
start_schedule_task() {
local agent_type=$1
local time_or_interval=$2
local cmd="python ${SCRIPT_DIR}/schedule_task.py"
# 如果同时执行两种智能体
if [ "${agent_type}" == "both" ]; then
cmd="${cmd} --both"
else
cmd="${cmd} --agent ${agent_type}"
fi
# 判断第二个参数是时间点还是间隔
if [[ "${time_or_interval}" =~ ^[0-9]+$ ]]; then
# 如果是纯数字,则认为是间隔
cmd="${cmd} --interval ${time_or_interval}"
else
# 否则认为是时间点
cmd="${cmd} --time \"${time_or_interval}\""
fi
# 使用nohup在后台运行
echo "启动定时任务: ${cmd}"
nohup ${cmd} > "${SCHEDULE_LOG}" 2>&1 &
# 保存PID
echo $! > "${SCRIPT_DIR}/.schedule_task.pid"
echo "定时任务已在后台启动PID: $!"
echo "日志文件: ${SCHEDULE_LOG}"
}
# 定义停止定时任务的函数
stop_schedule_task() {
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
local pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
echo "正在停止定时任务 (PID: ${pid})..."
kill -15 ${pid} 2>/dev/null || echo "进程 ${pid} 不存在"
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
else
echo "没有找到运行中的定时任务"
fi
}
# 定义查看状态的函数
check_status() {
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
local pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
if ps -p ${pid} > /dev/null; then
echo "定时任务正在运行 (PID: ${pid})"
echo "最近的日志:"
tail -n 20 "${SCHEDULE_LOG}"
else
echo "定时任务已停止 (上次PID: ${pid})"
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
fi
else
echo "没有找到运行中的定时任务"
fi
}
# 主逻辑
case "$1" in
start)
if [ -f "${SCRIPT_DIR}/.schedule_task.pid" ]; then
pid=$(cat "${SCRIPT_DIR}/.schedule_task.pid")
if ps -p ${pid} > /dev/null; then
echo "定时任务已在运行中 (PID: ${pid})"
echo "如需重启,请先执行: $0 stop"
exit 1
else
rm -f "${SCRIPT_DIR}/.schedule_task.pid"
fi
fi
# 检查必要的参数
if [ -z "$2" ]; then
echo "缺少参数: agent_type (crypto, gold 或 both)"
echo "用法: $0 start [agent_type] [time_or_interval]"
exit 1
fi
if [ -z "$3" ]; then
# 使用默认间隔
start_schedule_task "$2" "360"
else
start_schedule_task "$2" "$3"
fi
;;
stop)
stop_schedule_task
;;
status)
check_status
;;
*)
echo "用法: $0 {start|stop|status}"
echo " start [agent_type] [time_or_interval] - 启动定时任务"
echo " agent_type: crypto, gold 或 both"
echo " time_or_interval: 时间点(HH:MM)或间隔(分钟)"
echo " stop - 停止定时任务"
echo " status - 查看运行状态"
exit 1
;;
esac
exit 0