forked from DevOps/deploy.stack
feat(crontab): 添加时间同步脚本和安装文档
添加时间同步脚本 timeUpdate.job,支持通过 ntpdate 或 timedatectl 同步系统时间 同时添加 TIME_UPDATE_INSTALL.md 文档,详细说明安装配置步骤和常见问题解决方法
This commit is contained in:
38
crontab/timeUpdate.job
Executable file
38
crontab/timeUpdate.job
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user