使用shell实现高精度时间日志记录与时间跳变检测

news/2024/10/18 10:28:48/

文章目录

    • 0. 概述
    • 1. 使用说明
    • 1.1. 参数说明
      • 1.2. 运行脚本
    • 2. 脚本详细解析
      • 2.1. 参数初始化
      • 2.2. 参数解析与验证
      • 2.3 主循环条件
      • 2.4 时间跳变检测与处理
      • 2.5. 日志轮转机制
      • 2.6. 睡眠时间计算

0. 概述

之前写过单线程版本的高精度时间日志记录小程序:C++编程:实现简单的高精度时间日志记录小程序(单线程)
然后写了多线程版本的高精度时间日志记录小程序:使用C++实现高精度时间日志记录与时间跳变检测[多线程版本]

本文将使用shell脚本实现类似的功能,该脚本主要实现以下功能:

  1. 定时记录时间戳:按照指定的时间间隔(默认为20毫秒)记录当前时间戳
  2. 时间跳变检测与处理:当检测到系统时间回退或跳跃时,记录跳变前后的时间戳,以便后续分析。
  3. 日志轮转:当日志文件达到指定大小(默认为50MB)时,自动轮转日志文件,并保留一定数量的历史日志文件。
  4. 可配置参数:支持通过命令行参数自定义时间间隔、日志文件名、运行时长等配置。

1. 使用说明

1.1. 参数说明

脚本提供了多种可配置参数,用户可以根据需求进行调整:

  • -i, --interval <milliseconds>:设置记录时间戳的时间间隔,默认为20毫秒。
  • -f, --file <filename>:设置输出日志文件的名称,默认为timestamps_sh.txt
  • -t, --time <seconds>:设置脚本的运行时长,默认为72000秒(20小时)。
  • --disable_selective_logging:禁用选择性日志记录功能。
  • -h, --help:显示帮助信息。

1.2. 运行脚本

使用默认参数运行脚本:

./time_jump_check.sh

使用自定义参数运行脚本,例如设置间隔为500毫秒,运行时长为3600秒(1小时):

./time_jump_check.sh -i 500 -t 3600

禁用选择性日志记录功能:

./time_jump_check.sh --disable_selective_logging

2. 脚本详细解析

以下是time_jump_check.sh 脚本的完整代码:

#!/bin/bash# Default parameters
INTERVAL_MS=20               # Default interval 20 milliseconds
FILENAME="timestamps_sh.txt" # Default log file name
RUN_TIME_SECONDS=72000       # Default run time 72000 seconds (20 hours)
MAX_FILE_SIZE=50*1024*1024   # 50MB in bytes
SELECTIVE_LOGGING=true      # Default to enable selective logging
PRE_JUMP_RECORD=10          # Number of timestamps to record before jump
POST_JUMP_RECORD=10         # Number of timestamps to record after jump
MAX_LOG_FILES=2             # Maximum number of log files to keep# Function to show usage information
usage() {echo "Usage: $0 [options]"echo "Options:"echo "  -i, --interval <milliseconds>        Set the time interval, default is 20 milliseconds"echo "  -f, --file <filename>                Set the output file name, default is timestamps.txt"echo "  -t, --time <seconds>                 Set the run time, default is 72000 seconds (20 hours)"echo "      --disable_selective_logging      Disable selective logging feature"echo "  -h, --help                          Show this help message"exit 1
}# Parse command line arguments
while [[ $# -gt 0 ]]; dokey="$1"case $key in-i|--interval)INTERVAL_MS="$2"shift # past argumentshift # past value;;-f|--file)FILENAME="$2"shiftshift;;-t|--time)RUN_TIME_SECONDS="$2"shiftshift;;--disable_selective_logging)SELECTIVE_LOGGING=falseshift;;-h|--help)usage;;*)echo "Unknown option: $1"usage;;esac
done# Validate parameters
if ! [[ "$INTERVAL_MS" =~ ^[0-9]+$ ]] || [ "$INTERVAL_MS" -le 0 ]; thenecho "Error: Interval must be a positive integer."usage
fiif ! [[ "$RUN_TIME_SECONDS" =~ ^[0-9]+$ ]] || [ "$RUN_TIME_SECONDS" -le 0 ]; thenecho "Error: Run time must be a non-negative integer."usage
fi# Output configuration
echo "Time Interval: $INTERVAL_MS milliseconds"
echo "Output File: $FILENAME"
echo "Run Time: $RUN_TIME_SECONDS seconds"
echo "Selective Logging: $SELECTIVE_LOGGING"# Initialize variables
last_timestamp_us=0
second_last_timestamp_us=0
total_timestamps=0
in_jump_mode=false
jump_remaining=0
time_jump_count=0declare -a pre_jump_timestamps=()# Create or clear the log file
if ! > "$FILENAME"; thenecho "Error: Unable to create or clear log file '$FILENAME'."exit 1
fi# Main loop start time
START_TIME=$(date +%s)
while [[ $(($(date +%s) - START_TIME)) -lt $RUN_TIME_SECONDS || $time_jump_count -lt $RUN_TIME_SECONDS ]]; do# Get the current time stringcurrent_time_str=$(date +"%Y-%m-%d %H:%M:%S.%6N")# Convert current time to microseconds (since epoch)current_timestamp_us=$(date +"%s%6N")time_jump=falseif [[ $last_timestamp_us -ne 0 && $second_last_timestamp_us -ne 0 && $SELECTIVE_LOGGING == true ]]; thenif [[ $current_timestamp_us -lt $last_timestamp_us ]]; then# Time regression detectedtime_jump=trueelse# Check if the interval is too small based on second last timestampexpected_interval_us=$(( INTERVAL_MS * 1000 ))actual_interval_us=$(( current_timestamp_us - second_last_timestamp_us ))threshold_us=$(( expected_interval_us * 15 / 10 ))  # 1.5x thresholdif [[ $actual_interval_us -lt $threshold_us ]]; thentime_jump=truefififi# Update timestampssecond_last_timestamp_us=$last_timestamp_uslast_timestamp_us=$current_timestamp_us# Add current time to pre_jump_timestamps arraypre_jump_timestamps+=("$current_time_str")# Keep array length at most PRE_JUMP_RECORDif [[ ${#pre_jump_timestamps[@]} -gt $PRE_JUMP_RECORD ]]; thenpre_jump_timestamps=("${pre_jump_timestamps[@]: -$PRE_JUMP_RECORD}")fiif [[ $SELECTIVE_LOGGING == true && $time_jump == true && $in_jump_mode == false ]]; then# Detected a time jump, enter jump modein_jump_mode=truejump_remaining=$POST_JUMP_RECORD# Log pre-jump timestampsecho -e "\n--- TIME JUMP DETECTED ---" >> "$FILENAME"for ts in "${pre_jump_timestamps[@]}"; doecho "$ts" >> "$FILENAME"done# Log current timestamp with [TIME_JUMP] markerecho "$current_time_str [TIME_JUMP]" >> "$FILENAME"elif [[ $in_jump_mode == true ]]; then# In jump mode, record post-jump timestampsecho "$current_time_str" >> "$FILENAME"jump_remaining=$((jump_remaining - 1))if [[ $jump_remaining -le 0 ]]; thenin_jump_mode=falsefielse# Normal mode: log every 500 timestampstotal_timestamps=$((total_timestamps + 1))if [[ $((total_timestamps % 500)) -eq 0 ]]; thenecho "$current_time_str" >> "$FILENAME"fifi# Check and perform log rotationcurrent_size=$(stat -c%s "$FILENAME" 2>/dev/null || echo 0)if [[ $current_size -ge $MAX_FILE_SIZE ]]; thennew_filename="${FILENAME}.$(date +"%Y%m%d%H%M%S")"if ! mv "$FILENAME" "$new_filename"; thenecho "Error: Unable to rotate log file to '$new_filename'."exit 1fiecho "Rotated log file to $new_filename"# Create a new log fileif ! > "$FILENAME"; thenecho "Error: Unable to create new log file '$FILENAME'."exit 1fi# Remove oldest log file if there are more than MAX_LOG_FILESlog_files=($(ls -t "${FILENAME}".* 2>/dev/null))if [[ ${#log_files[@]} -gt $MAX_LOG_FILES ]]; thenfor (( i=$MAX_LOG_FILES; i<${#log_files[@]}; i++ )); doif ! rm "${log_files[$i]}"; thenecho "Error: Unable to remove old log file '${log_files[$i]}'."fidonefifi# Replace awk with bash and printf to calculate sleep_timeinteger_part=$(( INTERVAL_MS / 1000 ))fractional_part=$(( INTERVAL_MS % 1000 ))# Ensure fractional_part is three digits with leading zeros if necessaryfractional_part_padded=$(printf "%03d" "$fractional_part")sleep_time="${integer_part}.${fractional_part_padded}"# Sleep for the specified intervalsleep "$sleep_time"
doneecho "Program has ended."exit 0

2.1. 参数初始化

脚本开始部分定义了一系列默认参数,包括时间间隔、日志文件名、运行时长、最大日志文件大小、选择性日志记录开关、记录跳跃前后的时间戳数量以及最大保留的日志文件数量。

INTERVAL_MS=20               # 默认间隔20毫秒
FILENAME="timestamps_sh.txt" # 默认日志文件名
RUN_TIME_SECONDS=72000       # 默认运行时长72000秒(20小时)
MAX_FILE_SIZE=$((50*1024*1024))   # 50MB
SELECTIVE_LOGGING=true      # 默认启用选择性日志记录
PRE_JUMP_RECORD=10          # 跳跃前记录的时间戳数量
POST_JUMP_RECORD=10         # 跳跃后记录的时间戳数量
MAX_LOG_FILES=2             # 最大保留日志文件数量

2.2. 参数解析与验证

通过命令行参数,用户可以自定义脚本的运行参数。脚本使用while循环和case语句解析传入的参数,并进行必要的验证,确保参数的有效性。

# 解析命令行参数
while [[ $# -gt 0 ]]; dokey="$1"case $key in-i|--interval)INTERVAL_MS="$2"shift # 过去参数shift # 过去值;;-f|--file)FILENAME="$2"shiftshift;;-t|--time)RUN_TIME_SECONDS="$2"shiftshift;;--disable_selective_logging)SELECTIVE_LOGGING=falseshift;;-h|--help)usage;;*)echo "Unknown option: $1"usage;;esac
done# 验证参数
if ! [[ "$INTERVAL_MS" =~ ^[0-9]+$ ]] || [ "$INTERVAL_MS" -le 0 ]; thenecho "Error: Interval must be a positive integer."usage
fiif ! [[ "$RUN_TIME_SECONDS" =~ ^[0-9]+$ ]] || [ "$RUN_TIME_SECONDS" -le 0 ]; thenecho "Error: Run time must be a non-negative integer."usage
fi

2.3 主循环条件

主循环的条件为:

while [[ $(($(date +%s) - START_TIME)) -lt $RUN_TIME_SECONDS || $time_jump_count -lt $RUN_TIME_SECONDS ]]; do

这意味着,脚本将在以下两个条件之一满足时继续运行:

  1. 已经运行的时间未超过设定的RUN_TIME_SECONDS
  2. 检测到的时间跳变次数未超过RUN_TIME_SECONDS

这种设计确保了即使系统时间发生大幅跳变,脚本仍能继续运行,直到达到预定的运行时长。

2.4 时间跳变检测与处理

脚本通过比较当前时间戳与之前的时间戳来检测时间跳变。当检测到时间回退或时间间隔异常小时,触发跳变处理机制。

if [[ $current_timestamp_us -lt $last_timestamp_us ]]; then# 时间回退time_jump=truetime_jump_count=$((time_jump_count + 1))
else# 检查时间间隔是否异常expected_interval_us=$(( INTERVAL_MS * 1000 ))actual_interval_us=$(( current_timestamp_us - second_last_timestamp_us ))threshold_us=$(( expected_interval_us * 15 / 10 ))  # 1.5倍阈值if [[ $actual_interval_us -lt $threshold_us ]]; thentime_jump=truetime_jump_count=$((time_jump_count + 1))fi
fi

当检测到时间跳变时,脚本将:

  1. 记录跳变前的时间戳
  2. 标记当前时间戳为跳变时间。
  3. 在后续的循环中,记录一定数量的跳变后时间戳,确保日志的连续性。

2.5. 日志轮转机制

为防止日志文件过大,脚本实现了日志轮转功能。当日志文件大小超过MAX_FILE_SIZE时,脚本会:

  1. 将当前日志文件重命名为带有时间戳的文件名。
  2. 创建一个新的空日志文件。
  3. 保留最新的MAX_LOG_FILES个日志文件,删除最旧的文件。
# 检查并执行日志轮转
current_size=$(stat -c%s "$FILENAME" 2>/dev/null || echo 0)
if [[ $current_size -ge $MAX_FILE_SIZE ]]; thennew_filename="${FILENAME}.$(date +"%Y%m%d%H%M%S")"if ! mv "$FILENAME" "$new_filename"; thenecho "Error: Unable to rotate log file to '$new_filename'."exit 1fiecho "Rotated log file to $new_filename"# 创建新的日志文件if ! > "$FILENAME"; thenecho "Error: Unable to create new log file '$FILENAME'."exit 1fi# 如果日志文件超过MAX_LOG_FILES个,删除最旧的文件log_files=($(ls -t "${FILENAME}".* 2>/dev/null))if [[ ${#log_files[@]} -gt $MAX_LOG_FILES ]]; thenfor (( i=$MAX_LOG_FILES; i<${#log_files[@]}; i++ )); doif ! rm "${log_files[$i]}"; thenecho "Error: Unable to remove old log file '${log_files[$i]}'."fidonefi
fi

2.6. 睡眠时间计算

为了实现精确的时间间隔,脚本将INTERVAL_MS分解为整数部分和小数部分,并使用printf确保小数部分为三位数,最后组合成sleep命令所需的格式。

# 计算睡眠时间
integer_part=$(( INTERVAL_MS / 1000 ))
fractional_part=$(( INTERVAL_MS % 1000 ))
# 确保fractional_part为三位数,前面补零
fractional_part_padded=$(printf "%03d" "$fractional_part")
sleep_time="${integer_part}.${fractional_part_padded}"# 按指定间隔休眠
sleep "$sleep_time"

http://www.ppmy.cn/news/1538555.html

相关文章

通过Express + Vue3从零构建一个用户认证与授权系统(二)数据库与后端项目搭建与实现

前言 上一篇完成了系统的相关设计文档的编写&#xff0c;本文将详细介绍如何一步步使用 TypeScript 和 Express 搭建一个模块化、类型安全的用户认证与授权系统&#xff0c;包括数据库设计、后端项目搭建、用户认证、角色与权限管理、错误处理以及 Swagger 文档集成。 项目准…

MongoDB的增删改查

MongoDB数据库介绍 MongoDB是一个基于文档的NoSQL数据库&#xff0c;它以灵活的文档模型存储数据&#xff0c;非常适合现代应用程序的需求。 MongoDB 提供多种语言的驱动程序&#xff0c;包括Python、Java、C#、Node.js等。是一个基于分布式文件存储的开源数据库系统。将数据…

数学建模算法与应用 第4章 图与网络模型及其求解方法

目录 4.1 图的基本概念与数据结构 4.2 最短路径问题 4.3 最小生成树问题 4.4 网络最大流问题 4.5 图与网络模型的其他应用 习题 4 总结 图与网络模型是运筹学中的重要组成部分&#xff0c;用于描述和求解复杂系统中的连接和流动问题。在图与网络的框架下&#xff0c;许多…

thinkphp阿里云发送短信验证码,存储到缓存中完成手机号验证

源码demo下载地址 https://download.csdn.net/download/hanzhuhuaa/89865893 或者也可以在文章中查看demo源码 ↓ 第一步安装阿里云 SDK 您可以使用 Composer 来安装阿里云的 SDK。在项目目录中运行: composer require alibabacloud/sdk第二步发送短信 namespace app\api\c…

80.【C语言】数据结构之时间复杂度

目录 1.数据结构的定义 2.算法的定义 3.算法的效率 1.衡量一个算法的好坏的方法 例题:计算以下代码的循环次数 2.大O的渐进表示法 练习1:求下列代码的时间复杂度 练习2:求下列代码的时间复杂度 练习3:求下列代码的时间复杂度 练习4:求下列代码的时间复杂度 4.总结:计…

解决github每次pull push输入密码问题

# 解决git pull/push每次都需要输入密码问题 git bash进入你的项目目录&#xff0c;输入&#xff1a; git config --global credential.helper store然后你会在你本地生成一个文本&#xff0c;上边记录你的账号和密码。配置项写入到 "C:\Users\用户名\ .gitconfig" …

工具软件分享:11个免费的 android数据恢复应用程序功能分析

在手机上丢失数据是一个很大的错误。但是&#xff0c;在这种情况下&#xff0c;除了惊慌失措之外&#xff0c;最好开始使用android数据恢复应用程序搜索以查找将其取回的方法。您可以检查手机的备份存储以在Android上进行数据恢复&#xff0c;但是如果数据仍然无处可寻&#xf…

Android Studio 打包混淆失效问题

项目场景&#xff1a; 通过 Python 脚本运行打包 Apk &#xff0c;实现动态配置各个版本的 Apk。 问题描述 通过 Python 脚本打包编译 Apk&#xff0c;开启混淆后&#xff0c;打包成功&#xff0c;反编译出来的 Apk 并没有被混淆。 原因分析&#xff1a; 首先确认打包混淆…