(turn) Linux registration System Services-service command of the principle of popular __linux

Source: Internet
Author: User

The ability to operate with service commands is the system service that has been registered as Linux. Windows can also be registered as a method of system services.

Service command used a lot of times, is more related points, used many times, or some places did not get through, foggy. So just comb the technical knowledge point, straighten out the train of thought. At this point, I think it is necessary to understand the principle of the service command knowledge.

Understanding the ins and outs of service commands, help in solving practical problems, and practical applications, I can help you with the following questions:

1. If you download a new software from the Internet, install it to Linux. To register as a system service, I need to write a shell script file myself. How to write this script file. If you do not understand the mechanism of the Servcie command. Something's missing. For example, I downloaded the Sphinx before installing to the server. I want to manage with a short command such as "Service Sphinx start", which must be registered as a system service. And the source package does not provide the script to write well. You have to write one yourself.

Know the ins and outs, can reduce dependence. Like Niginx, MySQL we can write our own script files for Servcie calls.

2, the use of chkconfig prerequisites. If you want to further use the "Chkconfig--level 3456 Sphinx" from Control on and off, then you must implement a script file for invocation (that is, if the service can be invoked by a system service)

To understand the "why to register as a service" perspective, what is the benefit of doing so, from here to understand.

To register a program (such as MySQL, Apache, etc.) into the benefits of system services, popular point understanding I think that includes two aspects:

1, you can use the "service name" to manage, such as the often used command "service httpd start", that is, httpd registered as a service, so do not need to write a large list of the original service path.

Like Sphinx's Startup manual method is: "/data/installsoft/sphinx-for-chinese/bin/searchd–c profile"

Once the Sphinx is registered as a system service, it can be started like "service Sphinx start".

Note: Sphinx is a full-text search service. For the convenience of understanding, can be replaced by MySQL, Apache also line.

"Service Sphinx" as the equivalent of "/data/installsoft/sphinx-for-chinese/bin/searchd" can also be.

Related points

To register a program as a system service, you first have to give a script file for the service command call to the directory "/etc/rc.d/init.d/".

/etc/rc.d/init.d/This directory is actually a lot of script files. Look at the screenshot below, which is the file listed on my server (in fact, some shell script files)

Now the question: The script file is a shell file, and suppose you want to write the script yourself, what kind of content you want to write.

Lenovo often uses the command: Service httpd start, service httpd stop.

Registering as a system service is convenient like the above call. The shell script that is written should do what it takes to accept the start parameter, and what should be done to receive the stop parameter.

In fact, the content of the script can be written exactly as you want. For example, when I was managing Sphinx. I need to rebuild the index, directly passing a reindex parameter can be rebuilt: "Service Sphinx reindex".

the way of understanding popular point

I am accustomed to httpd "service httpd start" to the script file httpd under the directory/etc/rc.d/init.d/(the script file is the same as the service name), so the service httpd is to invoke/etc/rc.d/ The script file httpd under init.d/.

The next start, then, is to pass a start parameter to the/ETC/RC.D/INIT.D/HTTPD. summed up the following:

Service httpd Equivalence/etc/rc.d/init.d/httpd

Service httpd start equivalence/etc/rc.d/init.d/httpd start

Service httpd stop equivalent/etc/rc.d/init.d/httpd stop

Note: httpd is a shell script

Specific to this start parameter how to deal with, why do you like to do, you can completely stop the service, you can start the service. Just according to our habits, are starting services.

I generally understand that when the "service httpd start" command executes, a httpd script file will be found in the directory "/etc/rc.d/init.d/" directory. A file with the same name indicates that the service exists and does not prompt that the service does not exist. You can try a service that does not exist with a filename: Services jgj start to see what information will be prompted.

Actually, it's jgj. This file does not exist. Creating a jgj file in this directory will not tell you that the service does not exist. So the service exists under the condition that the file exists with the same name haha.

This time does not prompt the service does not exist, is does not have the execution permission

Add executive permission, chmod +x jgj

This time will execute the jgj script file inside the command, what inside is executes what, the syntax is wrong on the error.

Specific Example: Sphinx registration as a system service

I started the Sphinx service with the command "/data/install/sphinx-for-chinese/bin/searchd–c/data/install/sphinx-for-chinese/ect/ Shpinx.conf "

The command to rebuild the index in Sphinx is "/data/install/sphinx-for-chinese/bin/indexer–c–c/data/install/sphinx-for-chinese/ect/ shpinx.conf--all--rotate "

It is too long, other do not care what is doing, just need to know –c represents the configuration file location, which is followed by the value "/data/install/sphinx-for-chinese/ect/shpinx.conf"

If I need to operate this way every time, it's really tedious to knock commands. Add the Sphinx to the system service.

You can use the "service Sphinx start", "Service Sphinx reindex" very short command to replace a large list of the above.

The general meaning is that the above sequence of commands to start and rebuild the index are placed in the script file/etc/rc.d/init.d/sphinx (for the popular point, of course, not simply copied into the can, but also to receive the start, reindex these parameters)

Here is the name Sphinx script file I used on the server (/etc/rc.d/init.d/sphinx)

#!/bin/bash

# Chkconfig:-85 15

# Description:sphinx-for-chinese Service

#processname: Sphinx

Root_path=/data/installsoft/sphinx-for-chinese

Case "$" in

Start

echo "starging Sphinx Server ..."

$ROOT _path/bin/searchd-c $ROOT _path/etc/sphinx.conf

;;

Stop

echo "Stopping Sphinx Server ..."

$ROOT _path/bin/searchd--stop

;;

Restart)

$ROOT _path/bin/searchd--stop

$ROOT _path/bin/searchd-c $ROOT _path/etc/sphinx.conf

;;

Reindex

echo "Reindexing ..."

$ROOT _path/bin/indexer-c $ROOT _path/etc/sphinx.conf--all--rotate

;;

*)

echo "Usage: $ {Start|stop|restart|reindex}"

Exit 1

;;

Esac

Exit 0

Explanation: It uses case syntax. You can see that the receive start parameter comes in and executes the following: $ROOT _path/bin/searchd-c $ROOT _path/etc/sphinx.conf

Later I need to add some action items, such as Servcie Sphinx Kill, then continue to add a section of the script to receive the kill parameter of the processing code. By writing here, I understand more about registration as a convenient place for system services management.

2, registration into a system service, there is also a benefit. You can use the Chkconfig command to control the run level. That is, control what level below is open or run.

Chkconfig–level Sphinx 3456

This command is set at 3, 4, 5, 6 run level under the Sphinx service (that is,/etc/rc.d/init.d/sphinx this script) is the startup state.

I used to wonder, since the script files are used to/etc/rc.d/init.d/. The content of this script file is written entirely by itself, the contents of which do not necessarily have the start command AH. How to make the Chkconfig can boot service.

Each boot level has a separate directory under/etc/rc.d/: RC0.D, RC1.D.........RC6.D

Until the following is described in a material:

S-Starting file passes the start argument to the script

K-initiated file passes the Stop argument to the script

See the above words, immediately understand. It turns out that the start and stop arguments are passed to the/etc/rc.d/init.d/sphinx. In a word: Chkconfig configured S85sphinx These files are actually passing an argument to the script file/ect/rc.d/init.d/sphinx. This is very popular to understand. Chkconfig to boot mechanism, in the boot, pass parameters to the script file.

Now it is clear that the service script you are writing must also implement the process of accepting start and stop two parameters.

The relationship between service and Chkconfig is summarized as follows:

To register as a system service (that is, a service can be invoked), you can then use Chkconfig to control the run level.

Service is a prerequisite for chkconfig.



Original address:

Http://www.cnblogs.com/wangtao_20/archive/2014/04/04/3645690.html

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.