Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 環境下判斷伺服器是否正常關機

編輯:Linux 教學

原理:開機腳本建立標記檔 /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 腳本不會被執行。

Copyright © Windows教程網 All Rights Reserved