获取虚拟机运行时长的脚本

#!/bin/bash

# 获取虚拟机启动时间戳
boot_timestamp=$(date -d "$(uptime -s)" +%s)

# 当前时间戳
current_timestamp=$(date +%s)

# 计算运行时长(以秒为单位)
runtime=$((current_timestamp - boot_timestamp))

# 将运行时长转换为可读格式
days=$((runtime / 86400))
hours=$(( (runtime % 86400) / 3600 ))
minutes=$(( (runtime % 3600) / 60 ))
seconds=$((runtime % 60))

# 打印运行时长
echo "虚拟机已运行:$days 天 $hours 小时 $minutes 分钟 $seconds 秒"

 

posted @ 2023-07-18 15:10  吃吃吃大王  阅读(108)  评论(0)    收藏  举报