From 2cafa8715746a68f56b1a0cb864562458ea90eb8 Mon Sep 17 00:00:00 2001 From: wangdong_cmcc Date: Mon, 18 Nov 2024 11:20:03 +0800 Subject: [PATCH] add timesync check for os --- .../test04_timesync.sh | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh diff --git a/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh new file mode 100644 index 00000000..d2975089 --- /dev/null +++ b/source/tools/detect/generic/syscommand_check/syscommand_check_scripts/test04_timesync.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# check system time + +# 获取脚本所在目录 +OET_PATH=$(cd "$(dirname "$0")" || exit 1; pwd) +source "$OET_PATH/common_lib.sh" + +# 环境准备 +function pre_test() { + OLD_LANG=$LANG + export LANG=en_US.UTF-8 +} + +# 测试执行 +function run_test() { + # 检查系统时间 + current_time=$(date) + log_info "当前系统时间:$current_time" + + # 检查是否安装了chrony + if ! command -v chronyd &> /dev/null + then + log_info "chrony未安装,尝试安装chrony..." + sudo yum install -y chrony + fi + + # 启动chrony服务 + if ! systemctl is-active --quiet chronyd.service; then + sudo systemctl start chronyd.service + sleep 5 # 添加延迟,让chronyd完成初始化 + fi + + # 重新启动chrony服务以确保时间同步 + sudo systemctl restart chronyd.service + sleep 5 # 添加延迟,确保服务稳定运行 + + # 检查系统时间是否已经校正 + corrected_time=$(date) + log_info "校正后的系统时间:$corrected_time" +} + +# 环境清理 +function post_test() { + export LANG=${OLD_LANG} +} + +# 用例调用入口 +function main() { + pre_test + run_test + post_test + + # 返回测试结果 + if [ "$uname_passed" = true ]; then + return 0 + else + return 1 + fi +} + +# 脚本入口 +main "$@" -- Gitee