Add the shell script to the system service

Source: Internet
Author: User
Tags stop script

Have you ever felt that it's hard to write a long, absolute path when executing a script, to make it easy and easy to write your own script and start your footsteps as if you started Apache. (PS: Actually Apache is also a script)

First, to understand the basic knowledge and concepts

System operating level:

# 0-Stop (never set the Initdefault to 0)
# 1-Single user mode # s init s = init 1
# 2-Multiuser, no NFS
# 3-Full multi-user mode (standard run level)
# 4-No use
# 5-x11 Multi-user graphics mode (xwindow)
# 6-Reboot (never set Initdefault to 6)



Two. OK, get to know the next chkconfig.

Unlike DOS or Windows,linux, there can be multiple run levels. Common is multi-user 2,3,4,5, many people know that 5 is the level of running X-windows, and 0 is the shutdown. Changes to the run level can be toggled with the init command. For example, if you want to maintain the system into a single-user state, you can use init to switch. During the switching process of Linux running level, the system will automatically find files corresponding to the/etc/rc[0-6].d of K and S under the directory of Run level, and execute these scripts in the following numerical order. The maintenance of these scripts is a tedious thing, and Linux providesChkconfigCommands are used to update and query system services on different run levels.
The syntax is:
Chkconfig--list [Name]
Chkconfig--add Name
Chkconfig--del Name
Chkconfig[--level levels] Name
Chkconfig[--level levels] Name
ChkconfigThere are five features: adding services, deleting services, listing services, changing startup information, and checking the startup status of a particular service.
ChkconfigDisplays the usage when no parameters are run. If the service name is added, check that the service is started at the current run level. If yes, returns TRUE, otherwise false is returned. The--level option allows you to specify the run level you want to view, not necessarily the current run level.
If On,off or reset is specified after the service name, thenChkconfigWill change the startup information for the specified service. On and off refer to the start and stop of the service at the time of changing the operating level. Reset refers to initializing the service information, regardless of what is specified by the problematic initialization script.
For the on and off switches, the system is only valid for run Level 3, 4, and 5 by default, but reset can be valid for all run levels. When you specify the--level option, you can select a specific run level.
It is necessary to note that for each run level, there can be only one startup script or stop script. When you switch the run level, Init does not restart the service that is already started, nor does it stop the service that has stopped.
Option Description:
--level levels
Specifies the run level, a string consisting of numbers 0 through 7, such as:
--level 35 represents the specified run Level 3 and 5.
To deactivate the NFS service at run Level 3, 4, 5, use the following command:Chkconfig--level 345 NFS Off
--add Name
This option adds a new service,ChkconfigMake sure that each run level has a start (S) or Kill (K) entry. If one is missing, it is automatically created from the default init script.
--del Name
Used to delete the service and remove the associated symbolic connection from/ETC/RC[0-6].D.
--list Name
List, if name is specified, then only the specified service name is displayed, otherwise the state of all services at different run levels is listed.
Run-level files
Each isChkconfigThe managed service needs to add two or more lines of comments to the script under the corresponding INIT.D.
The first line tellsChkconfigThe default startup run level and the start and stop priority. If a service does not start at any run level by default, use-instead of run-level.
The second line describes the service and can be commented on across lines.



Three. OK, get to the chase. Write a script yourself (just demo, so super simple!) )

[Email Protected]_48_191_centos ~]# Cat 1.sh

#运行这个脚本, whether or not there is 123.txt, creates it and writes it to the inside in a cumulative way 1

#!/bin/bash

#

#description: A demo's description of the script doesn't really make sense.

#chkconfig: 2345 88 77 Start the script in S88 order at level 2,345 and close the script in K77 order at level 016

echo "1" >>/root/123.txt



Give this script permission and copy it to/ETC/INIT.D:

chmod a+x 123.txt

CP 123.TXT/ETC/INIT.D


Then perform the following:

[[Email protected]_48_191_centos ~]# service 1.sh start



It's a good success. Next, verify that the files have been generated under each level.



Four. Here are the figures

650) this.width=650; "title=" Pass.png "style=" Float:none "src=" https://s5.51cto.com/wyfs02/M01/95/34/ Wkiol1ksxnohzihqaadmi79b9s8755.png-wh_500x0-wm_3-wmp_4-s_1205823660.png "alt=" Wkiol1ksxnohzihqaadmi79b9s8755.png-wh_50 "/>

650) this.width=650; "title=" 2.png "style=" Float:none; "src=" https://s5.51cto.com/wyfs02/M02/95/34/ Wkiom1ksxnaixvy1aagyhvsmote320.png-wh_500x0-wm_3-wmp_4-s_365685719.png "alt=" Wkiom1ksxnaixvy1aagyhvsmote320.png-wh_50 "/>

650) this.width=650; "title=" 3.png "style=" Float:none; "src=" https://s2.51cto.com/wyfs02/M01/95/34/ Wkiom1ksxnfgi3hjaafxqafapuw071.png-wh_500x0-wm_3-wmp_4-s_1263870782.png "alt=" Wkiom1ksxnfgi3hjaafxqafapuw071.png-wh_50 "/>

650) this.width=650; "title=" 4.png "style=" Float:none; "src=" https://s1.51cto.com/wyfs02/M02/95/34/ Wkiom1ksxn2tg7tyaafyby3gtd8509.png-wh_500x0-wm_3-wmp_4-s_280499695.png "alt=" Wkiom1ksxn2tg7tyaafyby3gtd8509.png-wh_50 "/>

650) this.width=650; "title=" 5.png "style=" Float:none; "src=" https://s1.51cto.com/wyfs02/M00/95/34/ Wkiol1ksxoptnr4kaafyp-4vxmk654.png-wh_500x0-wm_3-wmp_4-s_3454345113.png "alt=" Wkiol1ksxoptnr4kaafyp-4vxmk654.png-wh_50 "/>

650) this.width=650; "title=" 6.png "style=" Float:none; "src=" https://s5.51cto.com/wyfs02/M00/95/35/ Wkiom1ksxoxyes84aafslqowfwg944.png-wh_500x0-wm_3-wmp_4-s_605409064.png "alt=" Wkiom1ksxoxyes84aafslqowfwg944.png-wh_50 "/>



This article is from the "Flying Small GUI" blog, make sure to keep this source http://9237101.blog.51cto.com/9227101/1924154

Add the shell script to the system service

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.