首页 技术分享 正文
  • 本文约426字,阅读需2分钟
  • 2509
  • 0

shell脚本记录开关机次数

1、开关机脚本内容

#!/bin/bash
# 检查记录文件是否存在,如果不存在则创建
if [ ! -f /var/log/shutdowns.log ]; then
    touch /var/log/shutdowns.log
    echo "0" > /var/log/shutdowns.log
fi
# 读取当前的开机次数
boot_count=$(cat /var/log/shutdowns.log)
# 记录开机时间和次数
echo "$(date): System booted. Shutdown count: $boot_count" >> /var/log/shutdowns.log
# 增加开机次数
((boot_count++))
# 将增加后的次数写入文件
echo "$boot_count" > /var/log/shutdowns.log
#输出i当前开关机次数
echo -n "Current shutdown count: $boot_count" 

2、将脚本加入到开机自启中


将脚本加入到 /etc/rc.local




    评论
    更换验证码
    友情链接