Centos Linux shutdown and service management (zt)

Source: Internet
Author: User
Tags egrep

This article is transferred from

(Super detail) http://bbs.ywlm.net/thread-615-1-1.html

Http://hi.baidu.com/edeed/blog/item/76b81e17dd9545054a90a705.html

Http://jiajun.iteye.com/blog/387265

Centos startup sequence:

1. When we press the power button to put the power into the machine, the BIOS (basicinput/Output System) program is first started and executed. The BIOS function is... And the MBR (Master Boot Record) with the first 512bit hard disk is accessed ). 2. The Boot Record in MBR is transferred to the memory through Bois control. Here we will talk about the Linux boot program. The Linux boot program includes grub and Lilo, while the centos default boot program is grub. 3. Access the Linux kernel program on the hard disk through the boot program. 4. Call the kernel program into the memory. 5. After the kernel program is called, access other files that will be used in the hard disk through the kernel. The first file to be executed by the kernel is/sbin/init, And the setting and definition file of this file is/etc/inittab, that is to say,/sbin/init should start the next step according to the definition of/etc/inittab, so we need to see what happened to the/etc/inittab file.

[Root @ Linux ~] # Cat-N/etc/inittab 1 #2 # inittab this file describes how the INIT process shoshould set up 3 # the system in a certain run-level. 4 #5 # Author: Miquel van smoorenburg,
<Miquels@drinkel.nl.mugnet.org> 6 # modified for RHS Linux by Marc Ewing and Donnie Barnes 7 #8 9 # default runlevel. the runlevels used by RHS are: 10 #0-halt (do not set initdefault to this) 11 #1-single user mode 12 #2-multiuser, without NFS (the same as 3, if you do not have networking) 13 #3-full multiuser mode 14 #4-unused 15 #5-X11 16 #6-Reboot (do not set initdefault t O this) 17 #18 ID: 3: initdefault: enabled default startup mode 19 20 # system initialization. 21 Si: sysinit:/etc/rc. d/RC. processing of sysinit reboot boot 22 23 l0: 0: Wait:/etc/rc. d/RC 0 rows 23-29. Start/etc/rc in each startup mode. d/rcX. d script 24 L1: 1: Wait:/etc/rc. d/RC 1 25 L2: 2: Wait:/etc/rc. d/RC 2 26 L3: 3: Wait:/etc/rc. d/RC 3 27 L4: 4: Wait:/etc/rc. d/RC 4 28 L5: 5: Wait:/etc/rc. d/RC 5 29 L6: 6: Wait:/etc/rc. d/RC 6 30 31 # trap CTRL-ALT-DELETE 32 CA: CTR Laltdel:/sbin/shutdown-T3-R now when CTRL + ALT + DEL is activated 33 34 # When our ups tells us power has failed, assume we have a few minutes 35 # of power left. schedule a shutdown for 2 minutes from now. 36 # This does, of course, assume you have powerd installed and your 37 # ups connected and working correctly. 38 PF: powerfail:/sbin/shutdown-f-h + 2 "power failure; system shutting down" handle power off 3 9 40 # If power was restored before the shutdown kicked in, cancel it. 41 PR: 12345: powerokwait:/sbin/shutdown-c "power restored; shutdown cancelled "handle power on 42 43 44 # Run Gettys in standard runlevels limit 45-50 rows are 6 virtual terminals 45 1: 2345: respawn: /sbin/mingetty tty1 46 2: 2345: respawn:/sbin/mingetty tty2 47 3: 2345: respawn:/sbin/mingetty tty3 48 4: 2345: respawn: /sbin/mingetty tty4 49 5: 2345: respawn:/sbin/min Getty tty5 50 6: 2345: respawn:/sbin/mingetty tty6 51 52 # Run xdm in runlevel 5 53 x: 5: respawn: start the/etc/X11 Window System [root @ Linux ~] in/etc/X11 window when/etc/X11/nodaemon restart mode 5. #

In the above file, the file starting with # Is a comment file, which can be ignored but can help us understand the file. The definition in this file is as follows: it is much clearer to see this file through the descriptions in the following table! <id>:<runlevel>:<action>:<process>

ID No repeated numbers
Runlevel 0 ~ 6 startup level (Mode)
Action
Initdefault Define the default Qidong level (Mode)
Sysinit Run in Boot
Wait Run the command once in the pilot until the INIT process ends.
Respawn When the process is stopped, restart
Powerfall When an abnormal power signal is received, run
Ctrlaltdel When the CTRL + ALT + DEL command is received, execute
Process

Define the executed command

 

Through the above description, I think you should be able to understand what is defined in/etc/inittab. Next we will focus on lines 18, 21, and 26 (other self-study ). We can see from the table above:

 

 

  • The 18 rows are defined as "3" text mode by default at the startup level.
  • The definition of line 21 is to execute/etc/rc during startup. d/RC. the sysinit file (the startup level is empty, that is to say, the file is executed at any level). This file has a length of about 1000 lines. Its main function is to start the network and read the file system, make swap efficient and call into modules.
  • The definition of the 26 rows corresponds to the definition of the 18 rows above, that is, the startup level of the 18 rows is "X ", then, execute the "X" defined in lines 23 to 29 of the file ".

Here, line 18 defines start Level 3. Execute the definition of start level 3 in line 26 and execute/etc/rc. d/RC Script File (control file), and hand over the startup level defined by 18 rows to/etc/rc. d/RC file for processing. Here we can see "L3: 3: Wait:/etc/rc. d/RC 3 "refers to handing over startup Level 3 to the file RC for processing. It is necessary to see what is defined in the RC file. The content of the/etc/rc. d/RC file is as follows:

[root@linux ~]# cat -n /etc/rc.d/rc     1   #! /bin/bash     2   #     3   # rc          This file is responsible for starting/stopping     4   #             services when the runlevel changes.              5   #     6   # Original Author:   
    7   #             Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>     8   #     9  10   # check a file to be a correct runlevel script 11   check_runlevel () 12   { 13       # Check if the file exists at all. 14       [ -x "$1" ] || return 1 15  16       # Reject backup files and files generated by rpm. 17       case "$1" in 18                *.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig) 19                          return 1 20                          ;; 21       esac 22       return 0 23   } 24  25   # Now find out what the current and what the previous runlevel are. 26   argv1="$1" 27   set `/sbin/runlevel` 28   runlevel=$2 29   previous=$1 30   export runlevel previous 31  32   . /etc/init.d/functions 33  34   # See if we want to be in user confirmation mode 35   if [ "$previous" = "N" ]; then 36       if [ -f /var/run/confirm ]; then 37                echo $"Entering interactive startup" 38       else 39                echo $"Entering non-interactive startup" 40       fi 41   fi 42  43   # Get first argument. Set new runlevel to this argument. 44   [ -n "$argv1" ] && runlevel="$argv1" 45  46   # Is there an rc directory for this new runlevel? 47   [ -d /etc/rc$runlevel.d ] || exit 0 48  49   # First, run the KILL scripts. 50   for i in /etc/rc$runlevel.d/K* ; do 51       check_runlevel "$i" || continue 52  53       # Check if the subsystem is already up. 54       subsys=${i#/etc/rc$runlevel.d/K??} 55       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \ 56                || continue 57  58       # Bring the subsystem down. 59       if egrep -q "(killproc |action )" $i ; then 60                $i stop 61       else 62                action $"Stopping $subsys: " $i stop 63       fi 64   done 65  66   # Now run the START scripts. 67   for i in /etc/rc$runlevel.d/S* ; do 68       check_runlevel "$i" || continue 69  70       # Check if the subsystem is already up. 71       subsys=${i#/etc/rc$runlevel.d/S??} 72       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \ 73                && continue 74                   75       # If we're in confirmation mode, get user confirmation 76       if [ -f /var/run/confirm ]; then 77                confirm $subsys 78                test $? = 1 && continue 79       fi 80  81       update_boot_stage "$subsys" 82       # Bring the subsystem up. 83       if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then 84                export LC_ALL=C 85                exec $i start 86       fi 87       if egrep -q "(daemon |action |success |failure )" $i 2>/dev/null \ 88                          || [ "$subsys" = "single" -o "$subsys" = "local" ]; then 89                $i start 90       else 91                action $"Starting $subsys: " $i start 92       fi 93   done 94   rm -f /var/run/confirm 95   if [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then 96 /usr/bin/rhgb-client --quit 97   fi [root@linux ~]#

In this file, let's look at the following lines:

  • 50 lines of script files starting with K
  • Execute stop in 60 rows
  • 67 lines of script files starting with S
  • Run start in line 91.

In this file, each part of the program block has a comment (the line starting with #). If you are interested, you can study it on your own. It seems that it is not so difficult, that is, check → do. After confirmation, execute, and then execute.
Files in the rc. d directory:

[Root @ Linux ~] # Ls-L/etc/rc. d Total usage 112 drwxr-XR-x 2 root Root 4096 December 28 12:45 init. d-rwxr-XR-x 1 Root 2352 December 28 RC drwxr-XR-x 2 root Root 4096 12:45 rc0.d drwxr-XR-x 2 root Root 4096 December 28 12:45 rc1.d drwxr-XR -x 2 root Root 4096 December 28 12:45 rc2.d drwxr-XR-x 2 root Root 4096 December 28 12:45 rc3.d drwxr-XR-x 2 root Root 4096 December 28 12:45 rc4.d drwxr-XR-x 2 root root 4096 December 28 12:45 rc5.d drw XR-x 2 root Root 4096 December 28 12:45 rc6.d-rwxr-XR-x 1 Root 220 RC. local-rwxr-XR-x 1 Root 27584 August 13 17:10 RC. sysinit [root @ Linux ~] #

We can see that below this directory, there are the RC files mentioned just now, and there are other files. Let's briefly describe them.
1. init. d. This is not a file, but a directory, which stores the control scripts of various services. The file below has something to do with what software packages you have installed. If you are interested, you can view their script files. We will talk about them in the following description, because/etc/rc. d/rcX. d file and the init. d. The files below are connected through soft connections.
2. the RC file is skipped as mentioned above.
3. RC. the loca file may be used. If you install some software or services, it is not a standard service of the system. For example, if you manually install httpd, you cannot use the chkconfig command to operate httpd, when the server is enabled, the HTTPd service is not started by default. At this time, you can write the startup command to this file, so that after the service is started, the HTTPd service will be started, note. The httpd mentioned here is just an example. In the future, many installed services may use this file. If you know it, you will naturally understand it when you need it. Of course, another method is to write a STARTUP script file by yourself so that the system can read the script file during startup to start httpd.
4. RC. sysini we mentioned above. This file is a script executed during boot. Its task is to initialize the network of the system, set the hostname, welcome message, and set the clock, mount a file system. If you are interested, you can read its script file.
5. rcX. d x in rcX. d Represents 0 ~ 6 (6 startup modes ). We can see that they are all directories, and the link of the file under/etc/rc. d/init. d we mentioned above is put below. Here we have rc3.d as an example to briefly describe it. When viewing this file, pay attention to the following two points: #1: ls-l view their detailed information to see if their link points to #2: ls-l check the headers and letters of their file names in the form of [s or k <number> <Name>. S indicates start, and K indicates stop. Do not confuse it./etc/rc. d/RC defines the "s" and "K" of the file name header text ". That is, when the system is started, do not run the script file starting with "S". This service will be started and run. Files starting with "K" will not be executed, the services controlled by this file are not executed, and the services controlled by this file are not started. In this case, we can directly change the corresponding startup mode (rc0.d ~ Rc6.d) change the file name of the corresponding service control script (change s to k, and change K to S) to easily optimize the services that the system will run during startup. If you use the command to control it, the command is the chkconfig command... editing ......
If you have viewed rc0.d ~ The name of the file under the rc6.d directory. You will find that the number of file names they get in S is different. /Etc/rc. d/rc3.d)

[Root @ Linux ~] # Ls-L/etc/rc. d/rc3.d 224 lrwxrwxrwx 1 Root 21 January 5 05:24 K01tog-pegasus-> .. /init. d/TOG-Pegasus lrwxrwxrwx 1 Root 13 January 5 05:12 k01yum-> .. /init. d/Yum lrwxrwxrwx 1 Root 24 January 5 05:12 k02networkmanager-> .. /init. d/NetworkManager lrwxrwxrwx 1 Root 15 January 5 05:12 k03rhnsd-> .. /init. d/rhnsd lrwxrwxrwx 1 Root 19 January 5 05:08 k05saslauthd-> .. /init. d/saslauthd lrwxrwxrwx 1 Root 16 January 5 05:11 k10psacct-> .. /init. d/psacct lrwxrwxrwx 1 Root 17 05:25 k12freewnn-> .. /init. d/freewnn lrwxrwxrwx 1 Root 13 January 5 05:12 k20nfs-> .. /init. d/nfs lrwxrwxrwx 1 Root 14 January 5 05:11 k24irda-> .. /init. d/IrDA lrwxrwxrwx 1 Root 16 January 5 05:10 k50ibmasm-> .. /init. d/ibmasm lrwxrwxrwx 1 Root 17 05:12 k50netdump-> .. /init. d/netdump lrwxrwxrwx 1 RO Ot root 16 January 5 05:26 k73ypbind-> ../init. d/ypbind [root @ Linux ~] #

Next, let's take a look at some tools for controlling services.Chkconfig setup system-config-Services let's take a look at these three tools respectively (if there are other tools, Let me know)
ChkconfigWe are familiar with this tool and have mentioned it in our website configuration explanation. Execution permission: root command path:/sbin/chkconfig usage: chkconfig <service name> On # this and next off are actually the scripts starting with K from the name chkconfig <service name> off # If you are interested in experimenting, you will know chkconfig -- list [service name] chkconfig -- add <service name> chkconfig -- del <service name> chkconfig [-- level <startup level>] <service name> <on | off | reset | resetpriorities>
SetupIs a Comprehensive System Configuration tool, which can also be used in the command line. Let's just talk about it. You will know what's going on.
System-config-servicesIf you have installed a Windows desktop system such as gnome, you can also use this tool to experience the convenience of clicking the mouse. (If you have installed the desktop system, you can add the tab key in # system-config. There will be many tools to execute commands .)
Main ServicesRefer: Http://www.centospub.com/bbs/viewthread.php? Tid = 1851 & extra = Page % 3d1
The above is a bit of knowledge about the startup process and service management of Linux (centos. Thank you very much for your support.

From: http://www.centospub.com/bbs/viewthread.php? Tid = 1295 & Highlight =

-- End --

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.