起因
有些服务器厂商的系统镜像不知道是什么原因,安装完系统之后不会自动校准时间,所以想自己写个脚本自动校准时间并设置时区。
功能
1. 完整的时区设置(Asia/Shanghai)
2. 使用systemd-timesyncd进行时间同步
3. 配置了多个可靠的NTP服务器(包括阿里云和腾讯云的服务器)
4. 包含详细的日志功能
5. 支持命令行参数和定时任务
6. 有完善的错误处理和状态检查
7. 包含彩色输出以提高可读性
8. 提供了帮助信息
代码
新建脚本:sync_shanghai_time.sh
将以下代码粘贴进脚本文件里:
#!/bin/bash
# 时间同步脚本 - 同步系统时间到上海时区
# 适用于Debian 11系统
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志文件
LOG_FILE="/var/log/time_sync.log"
# 日志函数
log() {
local level=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${timestamp} [${level}] ${message}" >> "${LOG_FILE}"
case ${level} in
"INFO")
echo -e "${GREEN}${timestamp} [${level}] ${message}${NC}"
;;
"WARNING")
echo -e "${YELLOW}${timestamp} [${level}] ${message}${NC}"
;;
"ERROR")
echo -e "${RED}${timestamp} [${level}] ${message}${NC}"
;;
*)
echo -e "${BLUE}${timestamp} [${level}] ${message}${NC}"
;;
esac
}
# 检查root权限
check_root() {
if [ "$EUID" -ne 0 ]; then
log "ERROR" "请使用root权限运行此脚本"
exit 1
fi
}
# 检查并安装必要的软件包
install_required_packages() {
log "INFO" "检查并安装必要的软件包..."
local packages_to_install=()
# 检查systemd-timesyncd
if ! dpkg -l | grep -q "^ii.*systemd-timesyncd"; then
packages_to_install+=("systemd-timesyncd")
fi
# 检查tzdata
if ! dpkg -l | grep -q "^ii.*tzdata"; then
packages_to_install+=("tzdata")
fi
# 如果有需要安装的包
if [ ${#packages_to_install[@]} -ne 0 ]; then
log "INFO" "正在安装以下软件包: ${packages_to_install[*]}"
apt-get update
apt-get install -y "${packages_to_install[@]}"
if [ $? -ne 0 ]; then
log "ERROR" "软件包安装失败"
exit 1
fi
else
log "INFO" "所有必要的软件包已安装"
fi
}
# 设置时区
set_timezone() {
log "INFO" "正在设置时区为Asia/Shanghai..."
# 检查当前时区
current_timezone=$(timedatectl show --property=Timezone --value)
if [ "$current_timezone" = "Asia/Shanghai" ]; then
log "INFO" "时区已经是Asia/Shanghai"
return 0
fi
# 设置时区
timedatectl set-timezone Asia/Shanghai
if [ $? -ne 0 ]; then
log "ERROR" "时区设置失败"
return 1
fi
log "INFO" "时区已设置为Asia/Shanghai"
return 0
}
# 配置时间同步服务
configure_time_sync() {
log "INFO" "配置时间同步服务..."
# NTP服务器列表
local ntp_servers=(
"ntp.aliyun.com"
"ntp1.aliyun.com"
"ntp2.aliyun.com"
"ntp3.aliyun.com"
"ntp4.aliyun.com"
"ntp5.aliyun.com"
"ntp6.aliyun.com"
"ntp7.aliyun.com"
)
# 创建timesyncd配置文件
cat > /etc/systemd/timesyncd.conf <<EOF
[Time]
NTP=${ntp_servers[*]}
FallbackNTP=time1.cloud.tencent.com time2.cloud.tencent.com time3.cloud.tencent.com time4.cloud.tencent.com time5.cloud.tencent.com
EOF
if [ $? -ne 0 ]; then
log "ERROR" "创建timesyncd配置文件失败"
return 1
fi
# 重启时间同步服务
systemctl restart systemd-timesyncd
if [ $? -ne 0 ]; then
log "ERROR" "重启时间同步服务失败"
return 1
fi
# 启用时间同步服务
systemctl enable systemd-timesyncd
if [ $? -ne 0 ]; then
log "ERROR" "启用时间同步服务失败"
return 1
fi
log "INFO" "时间同步服务配置完成"
return 0
}
# 同步时间
sync_time() {
log "INFO" "正在同步时间..."
# 等待时间同步
for i in {1..30}; do
if timedatectl show --property=NTPSynchronized --value | grep -q "yes"; then
log "INFO" "时间同步成功"
return 0
fi
sleep 1
done
log "ERROR" "时间同步超时"
return 1
}
# 显示当前时间信息
show_time_info() {
log "INFO" "当前时间信息:"
echo -e "${BLUE}----------------------------------------${NC}"
timedatectl status
echo -e "${BLUE}----------------------------------------${NC}"
}
# 检查时间同步状态
check_sync_status() {
local sync_status=$(timedatectl show --property=NTPSynchronized --value)
if [ "$sync_status" = "yes" ]; then
log "INFO" "时间同步状态: 已同步"
return 0
else
log "WARNING" "时间同步状态: 未同步"
return 1
fi
}
# 显示帮助信息
show_help() {
echo -e "${BLUE}Debian 11 上海时间同步脚本${NC}"
echo -e "用法: $0 [选项]"
echo
echo -e "选项:"
echo -e " -h, --help 显示此帮助信息"
echo -e " -c, --check 仅检查当前时间状态"
echo -e " -s, --sync 执行时间同步(默认行为)"
echo -e " -t, --timer 添加定时任务(每天凌晨2点执行)"
echo -e " -r, --remove 移除定时任务"
echo
echo -e "示例:"
echo -e " $0 执行时间同步"
echo -e " $0 --check 仅检查时间状态"
echo -e " $0 --sync --timer 执行同步并添加定时任务"
echo -e " $0 --remove 移除定时任务"
}
# 添加定时任务
add_cron_job() {
if ! crontab -l | grep -q "sync_shanghai_time.sh"; then
(crontab -l 2>/dev/null; echo "0 2 * * * /bin/bash $(readlink -f $0) --sync > /dev/null 2>&1") | crontab -
log "INFO" "已添加定时任务,脚本将每天凌晨2点自动执行"
else
log "INFO" "定时任务已存在"
fi
}
# 移除定时任务
remove_cron_job() {
if crontab -l | grep -q "sync_shanghai_time.sh"; then
crontab -l | grep -v "sync_shanghai_time.sh" | crontab -
log "INFO" "已移除定时任务"
else
log "INFO" "未找到相关定时任务"
fi
}
# 主函数
main() {
# 解析命令行参数
local SYNC=false
local CHECK=false
local TIMER=false
local REMOVE=false
# 如果没有参数,默认执行同步
if [ $# -eq 0 ]; then
SYNC=true
fi
# 解析参数
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-c|--check)
CHECK=true
shift
;;
-s|--sync)
SYNC=true
shift
;;
-t|--timer)
TIMER=true
shift
;;
-r|--remove)
REMOVE=true
shift
;;
*)
log "ERROR" "未知参数: $1"
show_help
exit 1
;;
esac
done
# 检查root权限
check_root
# 处理定时任务移除
if [ "$REMOVE" = true ]; then
remove_cron_job
exit 0
fi
# 仅检查状态
if [ "$CHECK" = true ]; then
show_time_info
check_sync_status
exit 0
fi
# 执行同步
if [ "$SYNC" = true ]; then
log "INFO" "开始时间同步流程..."
# 安装必要的软件包
install_required_packages
# 设置时区
set_timezone
if [ $? -ne 0 ]; then
log "ERROR" "时区设置失败,脚本终止"
exit 1
fi
# 配置时间同步服务
configure_time_sync
if [ $? -ne 0 ]; then
log "ERROR" "时间同步服务配置失败,脚本终止"
exit 1
fi
# 同步时间
sync_time
if [ $? -ne 0 ]; then
log "ERROR" "时间同步失败,脚本终止"
exit 1
fi
# 检查同步状态
check_sync_status
# 安装必要的软件包
install_required_packages
# 设置时区
set_timezone
if [ $? -ne 0 ]; then
log "ERROR" "时区设置失败,脚本终止"
exit 1
fi
# 配置时间同步服务
configure_time_sync
if [ $? -ne 0 ]; then
log "ERROR" "时间同步服务配置失败,脚本终止"
exit 1
fi
# 同步时间
sync_time
if [ $? -ne 0 ]; then
log "ERROR" "时间同步失败,脚本终止"
exit 1
fi
# 检查同步状态
check_sync_status
# 显示当前时间信息
show_time_info
log "INFO" "时间同步流程完成"
fi
# 添加定时任务(如果指定了-t或--timer选项)
if [ "$TIMER" = true ]; then
add_cron_job
fi
}
# 执行主函数
main "$@"
运行:
bash sync_shanghai_time.sh
© 版权声明
本站网络名称:
小怪兽
本站永久网址:
https://77il.cn
网站侵权说明:
本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ3031379629删除处理。
1 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
2 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
3 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
1 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
2 本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
3 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
THE END


- 最新
- 最热
只看作者