0

我正在使用 bash 脚本编写 Raspberry Pi,我想知道是否可以确定 RPi 重启了多少次。关键是我的程序正在做某事,如果我重新启动 3 次,它就会开始做其他事情。

我已经找到了这个https://unix.stackexchange.com/questions/131888/is-there-a-way-to-tell-how-many-times-my-computer-has-rebooted-in-a-24- hour-peri 但问题是它给了我一个不容易修改的数字。

有任何想法吗 ?

4

1 回答 1

1

感谢您的澄清。

last reboot | grep ^reboot | wc -l

这是您的系统进行的重新启动次数。由于您的程序不会在重新启动后“存活”,因此我假设您需要自程序第一次运行以来的重新启动次数。因此,您想存储第一次重新启动的次数,并在(第一次和)后续启动时重新读取:

if [[ ! -e ~/.reboots ]]
then
    echo $(last reboot | grep ^reboot | wc -l) > ~/.reboots
fi

INITIAL_REBOOTS=$(cat ~/.reboots)

# Now you can check if the *current* number of reboots
# is larger than the *initial* number by three or more:

REBOOTS=$(last reboot | grep ^reboot | wc -l)
if [[ $(expr $REBOOTS - $INITIAL_REBOOTS) -ge 3 ]]
then
    echo "Three or more reboots"
else
    echo "Less than three reboots"
fi

以上缺乏各种技巧和错误检查(例如,以防有人篡改~/.reboots),但仅作为概念证明。

于 2016-05-18T09:21:25.823 回答