Files
deploy.stack/crontab/timeUpdate.job

38 lines
1021 B
Plaintext
Raw Normal View History

#!/usr/bin/env bash
## crontab -e
## 0 1 * * * /home/geng/mydate/deploy.stack/crontab/timeUpdate.job >> /dev/null 2>&1
# 时间同步更新任务
# 尝试使用 ntpdate 更新时间
try_ntpdate() {
if command -v ntpdate &> /dev/null; then
echo "$(date): 使用 ntpdate 同步时间"
ntpdate -u cn.pool.ntp.org || ntpdate -u ntp.aliyun.com
return $?
fi
return 1
}
# 尝试使用 timedatectl 更新时间
try_timedatectl() {
if command -v timedatectl &> /dev/null; then
echo "$(date): 使用 timedatectl 同步时间"
timedatectl set-ntp true
return $?
fi
return 1
}
# 执行时间同步
if try_ntpdate; then
echo "$(date): 时间同步成功 (ntpdate)"
elif try_timedatectl; then
echo "$(date): 时间同步成功 (timedatectl)"
else
echo "$(date): 时间同步失败,未找到可用的时间同步工具"
exit 1
fi
# 可选:保存同步日志
echo "$(date): 系统时间已更新为: $(date)" >> /var/log/timeUpdate.log 2>&1