A simple method to determine whether a shutdown is normal in Linux

Source: Internet
Author: User
& Nbsp; idea: a file is generated when the system is started. when the system is shut down normally, the file is deleted. you can determine whether the file is shut down abnormally based on the file that is not used when the system is started. Step: program the script under/etc/rc. d/init. d. Determine whether the shutdown is normal, and the script that generates the File:  

Idea: A file is generated when the system is started. when the system is shut down normally, the file is deleted. you can judge whether the file is shut down abnormally based on the file that is not used during system startup.

Steps:

Program the script in/etc/rc. d/init. d.

Determine whether the shutdown is normal, and generate a file script: 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
        

Normal shutdown delete file script: rm-file.sh

        
         #!/bin/shfile=/tmp/checkecho "this is normal shutdown and rm file" >>/var/log/checkmessagerm -rf ${file}
        

Make the script run automatically.

        
         ln -s /etc/rc.d/rc0.d/K01rm-file /etc/rc.d/init.d/rm-file.sh
        

Note:

        
         #ll /etc/rc.d/rc0.d/
        

The names of the two sides of the soft connection are not the same. The latter can be an absolute or relative path, and the names of the two sides must be different.

        
         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
        

My system does not use x-window, so there is no level5, that is, there is no need to place scripts in/etc/rc. d/rc5.d.

Note:

Start with K indicates that the system is shut down, and start with S indicates that the system is executed at startup. Note the specification of server script writing, because the soft links opened by K may not be automatically executed when the server is shut down. why? When the K script is executed, it will query whether there is an empty file name with the same name as the script starting with K in/var/lock/subsys/. if not, it will not be executed, therefore, according to the specification written by the server script, you must first touch an empty file with the same name as K01 at/var/lock/subsys/startup:

        
         #/var/lock/subsys#touch fm-file
        
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.