Linux下判断是否正常关机的一个简单方法
;思想:系统启动的时候生成一个文件 , 正常关机的时候则删除这个文件 , 则可以根据系统开机时有没有那个文件来判断是不是非正常关机 。步骤: 在/etc/rc.d/init.d/下编程脚本 。判断是否正常关机 , 和生成文件的脚本:touch-file.sh #!/bin/shfile=/tmp/checkif [ -e ${file} ];thenecho "this is not normal shutdown" >>/var/log/checkmessageelseecho "this is normal shutdown and touch file" >>/var/log/checkmessagetouch ${file}fi正常关机删除文件的脚本:rm-file.sh #!/bin/shfile=/tmp/checkecho "this is normal shutdown and rm file" >>/var/log/checkmessagerm -rf ${file}让脚本自动执行 。ln -s /etc/rc.d/rc0.d/K01rm-file /etc/rc.d/init.d/rm-file.sh注意: #ll /etc/rc.d/rc0.d/软连接两边的名字不哪呢个相同 , 后一个可以用绝对路径或是相对路径 , 千万保证两边的名字不一样 。ln -s /etc/rc.d/rc6.d/K01rm-file /etc/rc.d/init.d/rm-file.shln -s /etc/rc.d/rc3.d/S01touch-file /etc/rc.d/init.d/touch-file.sh我的系统没用到x-window,所以没有level5 , 也就是不用在/etc/rc.d/rc5.d/下放脚本 。特别注意: K开头的代表系统关闭的时候执行 , S开头的代表开机的时候执行 。注意服务器脚本编写的规范 , 因为有K开通的软链接并不一定会在关机的时候自动去执行 , 这是为什么呢?执行K脚本的时候会查询/var/lock/subsys/下是否有与K开头脚本同名的空文件名 , 如果没有就不去执行 , 所以要按照服务器脚本编写的规范 , 启动的时候要在/var/lock/subsys/先touch一个与K01后面同名的空文件: #/var/lock/subsys#touch fm-file
推荐阅读
- Linux 核心--6.进程间通讯机制
- Linux 核心--5.Linux进程
- 网卡设置指南
- Linux下构架qmail邮件系统
- linux的运行模式:runlevel
- 用 Linux 打造路由器
- Linux 和 Windows 共享交换区
- Linux下的常用软件列表
- 优化Linux系统硬盘的七个实用技巧
- Linux系统中提取DVD音频的方法介绍