Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Add the shell script to the system service

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Do you ever find it hard to write a long absolute path when executing a script? to make it easy and easy, run your script like a service and start your footsteps like starting apache. (PS: actually, apache is also a script)

one。 First of all, understand the basic knowledge and concepts.

System run level:

# 0-stop (never set initdefault to 0)

# 1-single user mode # s init s = init 1

# 2-Multi-user, no NFS

# 3-full multiuser mode (standard runlevel)

# 4-not used

# 5-X11 Multiuser Graphics Mode (xwindow)

# 6-restart (never set initdefault to 6)

Two. OK, let's learn more about chkconfig

Unlike DOS or Windows,Linux, there can be multiple runlevels. What is common is the multi-user 2meme 3meme 4je 5. Many people know that 5 is the level at which X-Windows is run, and 0 is the shutdown. Run-level changes can be switched through the init command. For example, if you want to keep the system in a single-user state, you can use init 1 to switch. During the run-level switching of Linux, the system automatically looks for the files starting with K and S under the corresponding runtime directory / etc/rc [0-6] .d, and executes these scripts in the following numerical order. Maintaining these scripts is tedious, and Linux provides chkconfig commands to update and query system services at different runtime levels.

The syntax is:

Chkconfig-- list [name]

Chkconfig-add name

Chkconfig-del name

Chkconfig [--level levels] name

Chkconfig [--level levels] name

Chkconfig has five functions: add services, delete services, list services, change startup information, and check the startup status of specific services.

Chkconfig displays usage when it runs with no parameters. If you add the service name, check to see if the service is started at the current run level. If yes, return true, otherwise return false. The-- level option specifies the runlevel to view, not necessarily the current runlevel.

If on,off or reset is specified after the service name, chkconfig changes the startup information of the specified service. On and off refer to the start and stop of a service when the runlevel is changed, respectively. Reset refers to initialization service information, no matter what is specified in the problematic initialization script.

For on and off switches, the system defaults to only runlevel 3, 4, 5, but reset is valid for all runlevels. When you specify the-- level option, you can select a specific runlevel.

It is important to note that there can be only one start script or stop script per runlevel. When you switch runlevels, init does not restart services that have been started, nor does it stop services that have been stopped.

Description of options:

-- level levels

Specifies the run level, a string consisting of the numbers 0 to 7, such as:

-- level 35 indicates that runlevels 3 and 5 are specified.

To deactivate the nfs service at runlevels 3, 4, and 5, use the following command: chkconfig-- level 345 nfs off

-- add name

This option adds a new service, and chkconfig ensures that each runlevel has a startup (S) or kill (K) entry. If it is missing, it is automatically created from the default init script.

-- del name

Used to delete the service and remove the associated symbolic link from / etc/rc [0-6] .d.

-- list name

List, if name is specified, then only the specified service name is displayed, otherwise, the status of all services at different runlevels is listed.

Run-level file

Each service managed by chkconfig needs to add two or more lines of comments to the script under the corresponding init.d.

The first line tells chkconfig the default run level to start and the priority to start and stop. If a service is not started at any runtime level by default, use-instead of runlevel.

The second line describes the service, which can be commented across lines.

three。 All right, let's get down to business. Write your own script (it's just a demonstration, so it's super easy! )

[root@VM_48_191_centos ~] # cat 1.sh

# run this script, regardless of whether there is a 123.txt or not, it will be created and 1 will be written accumulatively to it

#! / bin/bash

#

# description: a demo's description of this script actually doesn't make much sense

# chkconfig:2345 88 77 will look for the S881.sh file at the 2345 level, go to / etc/init.d/1.sh to start the service, K771.sh the file at the 016 level, and turn off the service through the soft link / etc/init.d/1.sh, so these 88 and 77 can actually be written at will, even if the two scripts write the same 88 and 77 will not conflict Because its naming rule under rc2.d is to start (S881.sh) or stop (K771.sh), the script name is unique, so there is no conflict.

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

Grant permission to this script and copy it to / etc/init.d:

Chmod axix 1.sh

Cp 1.sh / etc/init.d/

Add to the system service:

Chkconfig-add 1.sh

Then execute the following:

[root@VM_48_191_centos ~] # service 1.sh start

Okay, it worked. Next, verify that files have been generated at each level.

four。 These are all pictures.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report