原理:開機腳本建立標記檔 /tmp/check,正常關機腳本刪除該檔。下次開機檢查檔案是否存在,即可判斷上次是否非正常關機,記錄至 /var/log/checkmessage。
touch-file.sh#!/bin/sh
file=/tmp/check
if [ -e $file ]; then
echo "this is not normal shutdown" >> /var/log/checkmessage
else
echo "this is normal shutdown and touch file" >> /var/log/checkmessage
touch $file
fi
rm-file.sh#!/bin/sh
file=/tmp/check
echo "this is normal shutdown and rm file" >> /var/log/checkmessage
rm -rf $file
ln -s /etc/rc.d/init.d/touch-file.sh /etc/rc.d/rc3.d/S01touch-file
ln -s /etc/rc.d/init.d/rm-file.sh /etc/rc.d/rc0.d/K01rm-file
ln -s /etc/rc.d/init.d/rm-file.sh /etc/rc.d/rc6.d/K01rm-file
注意:K 開頭關機執行,S 開頭開機執行。需在 /var/lock/subsys/ 建立同名空檔 touch rm-file,否則 K 腳本不會被執行。