Files
jd_wl_python/run_logistics.sh
2026-04-26 13:55:54 +08:00

105 lines
3.0 KiB
Bash
Raw Permalink 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.
#!/usr/bin/env bash
# =============================================================================
# 物流提取脚本 - systemd 优化版
# =============================================================================
#
# 同机多实例(并行查询):每个进程独立 Chrome进程内仍是串行抓取不是「单进程多线程并行」。
# Java 侧配置逗号分隔多个地址,例如:
# jarvis.server.logistics.base-urls=http://127.0.0.1:5001,http://127.0.0.1:5002,http://127.0.0.1:5003
#
# 三端口示例(建议每实例单独 LOG_FILE避免日志交错
# LOGISTICS_PORT=5001 LOG_FILE="${PWD}/logistics-5001.log" ./run_logistics.sh
# LOGISTICS_PORT=5002 LOG_FILE="${PWD}/logistics-5002.log" ./run_logistics.sh
# LOGISTICS_PORT=5003 LOG_FILE="${PWD}/logistics-5003.log" ./run_logistics.sh
#
# systemd复制三份 service分别设置 Environment=LOGISTICS_PORT=500x 与不同的日志路径。
# =============================================================================
set -euo pipefail
# 配置部分
readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
readonly VENV_DIR="${VENV_DIR:-${SCRIPT_DIR}/venv}"
readonly PYTHON_SCRIPT="${PYTHON_SCRIPT:-${SCRIPT_DIR}/jd/fetch_logistics_ubuntu.py}"
readonly LOG_FILE="${LOG_FILE:-${SCRIPT_DIR}/logistics.log}"
# 供 jd/fetch_logistics_ubuntu.py 读取(默认 5001
export LOGISTICS_PORT="${LOGISTICS_PORT:-5001}"
# 日志函数
log_info() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $*" | tee -a "${LOG_FILE}"
}
log_error() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $*" | tee -a "${LOG_FILE}" >&2
}
# 检查文件是否存在
check_file() {
if [[ ! -f "$1" ]]; then
log_error "文件不存在: $1"
return 1
fi
return 0
}
# 检查目录是否存在
check_dir() {
if [[ ! -d "$1" ]]; then
log_error "目录不存在: $1"
return 1
fi
return 0
}
# 主函数
main() {
log_info "开始执行物流提取脚本 (LOGISTICS_PORT=${LOGISTICS_PORT})"
# 切换到脚本目录
cd "${SCRIPT_DIR}" || {
log_error "无法切换到目录: ${SCRIPT_DIR}"
return 1
}
# 检查虚拟环境
if ! check_dir "${VENV_DIR}"; then
return 1
fi
# 检查Python脚本
if ! check_file "${PYTHON_SCRIPT}"; then
return 1
fi
# 激活虚拟环境
log_info "激活虚拟环境: ${VENV_DIR}"
source "${VENV_DIR}/bin/activate" || {
log_error "无法激活虚拟环境"
return 1
}
# 检查Python版本
python_version=$(python --version 2>&1)
log_info "Python版本: ${python_version}"
# 运行Python脚本
log_info "执行脚本: ${PYTHON_SCRIPT}"
# 传递所有参数给Python脚本
if python "${PYTHON_SCRIPT}" "$@"; then
log_info "脚本执行成功"
return 0
else
log_error "脚本执行失败"
return 1
fi
}
# 执行
if main "$@"; then
exit 0
else
exit 1
fi