From d8e2dd44a867381866d6deacdb0a93c9c5481f73 Mon Sep 17 00:00:00 2001 From: fanhuadesenlinnn Date: Tue, 16 Sep 2025 13:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20log=5Fcmd.demo.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log_cmd.demo.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 log_cmd.demo.sh diff --git a/log_cmd.demo.sh b/log_cmd.demo.sh new file mode 100644 index 0000000..5b0d91b --- /dev/null +++ b/log_cmd.demo.sh @@ -0,0 +1,22 @@ +#!/bin/env bash +LOGFILE="/root/app/init.log" +PG_DOCKER=/lzcsys/bin/pg-docker +# 执行并记录日志 + 耗时 +log_cmd() { + local start end elapsed + echo "##### 执行命令: $* $(date +%Y%m%d-%H%M-%S)" | tee -a "$LOGFILE" + + start=$(date +%s%N) + + # 执行命令,输出到屏幕同时写入日志 + "$@" 2>&1 | tee -a "$LOGFILE" + + end=$(date +%s%N) + elapsed=$(( (end - start)/1000000 )) # 毫秒 + + echo "⏱️ 耗时: ${elapsed} ms" | tee -a "$LOGFILE" + echo "" | tee -a "$LOGFILE" +} + +log_cmd apt update +log_cmd apt install -y wget tmux htop git \ No newline at end of file