Files
openresty-gateway/scripts/install-renew-cron.sh
2026-05-18 22:32:08 +08:00

37 lines
1.1 KiB
Bash
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.
#!/usr/bin/env sh
set -eu
# 安装证书自动续期定时任务。
# 默认每天 03:00 执行 scripts/renew-certs.sh并把日志写到 logs/cert-renew.log。
# 可以重复执行:安装前会先删除旧的 openresty-gateway cert renew 任务块,再写入新的任务。
MARK_BEGIN="# BEGIN openresty-gateway cert renew"
MARK_END="# END openresty-gateway cert renew"
SCHEDULE="${SCHEDULE:-0 3 * * *}"
ROOT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
LOG_DIR="$ROOT_DIR/logs"
CRON_LINE="$SCHEDULE cd '$ROOT_DIR' && sh scripts/renew-certs.sh >> logs/cert-renew.log 2>&1"
if ! command -v crontab >/dev/null 2>&1; then
echo "错误:缺少 crontab 命令。" >&2
exit 1
fi
mkdir -p "$LOG_DIR"
tmp_file="$(mktemp)"
trap 'rm -f "$tmp_file"' EXIT
echo "删除已存在的证书续期 cron 块(如果存在)..."
crontab -l 2>/dev/null | sed "/$MARK_BEGIN/,/$MARK_END/d" > "$tmp_file"
{
echo "$MARK_BEGIN"
echo "$CRON_LINE"
echo "$MARK_END"
} >> "$tmp_file"
crontab "$tmp_file"
echo "已安装证书续期 cron"
echo "$CRON_LINE"