Run-level Script 2

Source: Internet
Author: User

The previous "Run-level script" was designed to visually understand the Run-level script. This article mainly records some practical knowledge.

I. Structure of running-level scripts
[root@localhost rc0.d]# cat /etc/init.d/!/bin/--   =  ] && exit  [ -n -n $            /usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/=$?-eq  ] && success || [ $RETVAL -eq  =  ] &&=-g can replace the grep =-n $=$?-eq  ] && touch ///subsys/-n $=$?-eq  ] && rm -f ///subsys/  =$?|=$? [ -f ///subsys/=$?*=

This is a very long script. only part of the script is intercepted above. This part exactly shows how to write the running-level script.

(1) first, you must declare the default running level list of the service in the form of comments. The priority of the Service Startup and shutdown when the system is started or disabled.

(2) generally, the Service Startup and shutdown operations are written in the function, and then called as a function.

(3) Use the case statement to process parameters such as strart and stop.

(4) handle unsuitable parameters at the end of the case statement.

(5) If you need to check whether the service is running, it is best to create a running Tag file.

Note: When setting the priority of running-level scripts to start or stop, pay attention to the priority of other services on which the script depends to avoid service startup failure.

2. Compile a running-level script

The purpose of writing a running-level script may be to add a new startup method for the monitoring script you have prepared, or to compile a startup service for a product. An example script is introduced here (there is no substantive content. You need to add the actual statements for starting or disabling services in the function during actual writing ):

! /Bin/, and the startup and shutdown priorities are 80 and. $. | *

Note: The two lines starting with "# chkconfig" and "# description" must be defined in the running-level script. They are mainly used to initialize the configuration and description when adding services.

3. Add and manage run-level scripts

The purpose of writing a running-level script is to add the script to the system as a service.

1. Add a running-level script to serve the system

Before adding a Run-level script as a system service, copy the compiled run-level script to the/etc/init. d directory and add the corresponding execution permissions. After that, you can use the chkconfig command to add the running-level scripts as system services.

Run the following command to add the sample script test as a system service:

[root@localhost shell]# chkconfig --add test

After successful addition, the command does not return any information. Run the following command to view the service:

[root@localhost shell]# chkconfig --:off   :off   :off   :on    :on    :on    --list |:off   :off   :off   :on    :on    :on    :off

In addition, after the chkconfig-add test command is used to add test to the system service, we can find that the startup and closing scripts of test are automatically added to the directories at various running levels, and these scripts are all/etc/init. d/test link file:

[root@localhost shell]# ls -al /etc/rc.d/rc0.d | root root    Dec  : K10test -> ../init.d/-al /etc/rc.d/rc1.d | root root    Dec  : K10test -> ../init.d/-al /etc/rc.d/rc2.d | root root    Dec  : K10test -> ../init.d/-al /etc/rc.d/rc3.d | root root    Dec  : S80test -> ../init.d/-al /etc/rc.d/rc4.d | root root    Dec  : S80test -> ../init.d/-al /etc/rc.d/rc5.d | root root    Dec  : S80test -> ../init.d/-al /etc/rc.d/rc6.d | root root    Dec  : K10test -> ../init.d/test

2. Manage to add to service by running-level scripts

After you use the chkconfig command to add a service, you can use the Command service to start the service:

/etc/init.d/test:

Of course, you can also delete it at any time:

[root@localhost shell]# chkconfig --del test

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.