1
This commit is contained in:
36
scripts/install-renew-cron.sh
Normal file
36
scripts/install-renew-cron.sh
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/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"
|
||||
Reference in New Issue
Block a user