forked from DevOps/deploy.stack
feat(crontab): 添加时间同步脚本和安装文档
添加时间同步脚本 timeUpdate.job,支持通过 ntpdate 或 timedatectl 同步系统时间 同时添加 TIME_UPDATE_INSTALL.md 文档,详细说明安装配置步骤和常见问题解决方法
This commit is contained in:
134
crontab/TIME_UPDATE_INSTALL.md
Normal file
134
crontab/TIME_UPDATE_INSTALL.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# 时间更新脚本 - Linux CLI 工具安装指南
|
||||
|
||||
本文档提供了安装和配置时间更新脚本(`timeUpdate.job`)所需的 Linux 命令行工具的详细步骤。
|
||||
|
||||
## 系统要求
|
||||
|
||||
- 操作系统:Linux(Ubuntu/Debian/CentOS/RHEL等)
|
||||
- root权限(运行时间同步命令时需要)
|
||||
|
||||
## 所需 CLI 工具
|
||||
|
||||
时间更新脚本支持两种时间同步方式,需要以下工具之一:
|
||||
|
||||
1. `ntpdate` - 传统的 NTP 时间同步工具
|
||||
2. `timedatectl` - systemd 系统的时间管理工具
|
||||
|
||||
## 工具安装方法
|
||||
|
||||
### 安装 ntpdate
|
||||
|
||||
#### Ubuntu/Debian 系统
|
||||
```bash
|
||||
sudo apt-get update
|
||||
sudo apt-get install ntpdate
|
||||
```
|
||||
|
||||
#### CentOS/RHEL 系统
|
||||
```bash
|
||||
sudo yum install ntpdate
|
||||
```
|
||||
|
||||
#### Fedora 系统
|
||||
```bash
|
||||
sudo dnf install ntpdate
|
||||
```
|
||||
|
||||
### 安装 timedatectl
|
||||
|
||||
timedatectl 是 systemd 的一部分,大多数现代 Linux 发行版都已预装。如果您的系统中没有,可以按以下方式安装:
|
||||
|
||||
#### Ubuntu/Debian 系统
|
||||
```bash
|
||||
sudo apt-get update
|
||||
sudo apt-get install systemd
|
||||
```
|
||||
|
||||
#### CentOS/RHEL 系统
|
||||
```bash
|
||||
sudo yum install systemd
|
||||
```
|
||||
|
||||
#### Fedora 系统
|
||||
```bash
|
||||
sudo dnf install systemd
|
||||
```
|
||||
|
||||
## 验证工具安装
|
||||
|
||||
### 验证 ntpdate
|
||||
|
||||
安装完成后,可以使用以下命令验证 ntpdate 是否正确安装:
|
||||
```bash
|
||||
which ntpdate
|
||||
ntpdate --version
|
||||
```
|
||||
|
||||
### 验证 timedatectl
|
||||
|
||||
使用以下命令验证 timedatectl 是否正确安装:
|
||||
```bash
|
||||
which timedatectl
|
||||
timedatectl --version
|
||||
```
|
||||
|
||||
## 配置时间同步服务器
|
||||
|
||||
时间更新脚本默认使用以下 NTP 服务器:
|
||||
1. `cn.pool.ntp.org` - 中国 NTP 服务器池
|
||||
2. `ntp.aliyun.com` - 阿里云 NTP 服务器(备用)
|
||||
|
||||
您可以根据需要在脚本中修改这些服务器地址。
|
||||
|
||||
## 添加定时任务
|
||||
|
||||
要使时间更新脚本定期运行,需要将其添加到系统的 crontab 中:
|
||||
|
||||
1. 以 root 用户身份编辑 crontab:
|
||||
```bash
|
||||
sudo crontab -e
|
||||
```
|
||||
|
||||
2. 添加以下行(每天凌晨1点执行):
|
||||
```
|
||||
0 1 * * * /home/geng/mydate/deploy.stack/crontab/timeUpdate.job >> /dev/null 2>&1
|
||||
```
|
||||
|
||||
3. 保存并退出编辑器。
|
||||
|
||||
## 手动运行脚本
|
||||
|
||||
如果需要立即执行时间同步,可以手动运行脚本:
|
||||
```bash
|
||||
sudo /home/geng/mydate/deploy.stack/crontab/timeUpdate.job
|
||||
```
|
||||
|
||||
## 查看同步日志
|
||||
|
||||
时间同步的结果会记录在以下日志文件中:
|
||||
```bash
|
||||
tail -f /var/log/timeUpdate.log
|
||||
```
|
||||
|
||||
如果日志文件不存在,系统会自动创建它。
|
||||
|
||||
## 常见问题解决
|
||||
|
||||
### 1. 权限错误
|
||||
|
||||
如果运行脚本时遇到权限错误,请确保以 root 用户身份运行,或使用 sudo 命令。
|
||||
|
||||
### 2. 网络连接问题
|
||||
|
||||
如果时间同步失败,可能是网络连接问题。请检查您的网络连接和防火墙设置,确保允许访问 NTP 服务器的 UDP 123 端口。
|
||||
|
||||
### 3. 工具不可用
|
||||
|
||||
如果两种工具都不可用,脚本会输出错误信息。请确保至少安装了其中一种工具。
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 时间同步可能需要 root 权限,因此请确保以适当的权限运行脚本。
|
||||
2. 在生产环境中,建议先在测试环境验证脚本的功能。
|
||||
3. 定期检查时间同步日志,确保时间同步正常工作。
|
||||
4. 如果您的系统在防火墙后面,可能需要配置防火墙规则以允许 NTP 流量。
|
||||
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