Linux_ the executable program into a service __linux

Source: Internet
Author: User

To create a service for an executable program

When the Linux system is started, we can see that a lot of service programs are started one by one (that is, those with a blue [OK] line behind them), and these programs that run in the background play an important role in using the computer, and the programs that run in this way have the following characteristics:
Boot up, shutdown stop (nonsense. Shut down the machine also run AH ... )
Background run
Control or view the running state of a program through a service command

Well, that's right, so how do we make our own program into such a service?

Needless to say, the main points are the following 3 steps:

1, put the executable program to a Linux system can be found in the place.
At the Linux command prompt, enter env, you can see the environment variables, there is a path, yes, the directory specified in this is the system can find place, our executable program into one of the directories, we generally put in the/usr/sbin/ The directory (put what has put what place, it is best not to put it in a disorderly way).

2. Under the directory/etc/init.d/, create a new file with the name of the name of the service.
If we open the directory/etc/init.d/, see the file is actually a service program file, each file is the same content, we will see that the file in the file structure is almost the same. Almost every file has a flag such as Start, stop, restart, and status, and we have the same structure for this new file, just to replace the corresponding executable program with our own executable program within these flags, and know what to do. That is: copy--Modify--save.

3. Under directory/etc/rc3.d/, create a new symbolic connection file that points to the service file created in step 2nd.
Open the directory/etc/rc3.d/and we'll see that this is all symbolic connection files (symbolic connections in Linux are equivalent to shortcuts in Windows). Use the following command format:
LN-SF destination file name connection file name
The connection file name benefits from the "sxx target filename" format, XX is generally an integer from 1 to 100, which indicates the start priority, the larger the number, the lower the priority, such as: service a operation to rely on service B, that A's XX number should be greater than B. Followed by the "target file name" is to see at a glance, you know which file is the symbolic connection.
The directory/etc/rc3.d/is a directory that is automatically searched when the system starts up, and the target files of the symbolic connection files in the directory will be run, which is why the symbolic connection is established in this directory--to run on the boot.

Well, with the above 3 steps, you're going to make one of your executable programs a service to the system, which has a few specific ones that we started with. We can use the Service command to control this program:
Start: Service Hdz_service start
STOP: Service Hdz_service stop
Restart: Service Hdz_service restart
The hdz_service above is the service name, which is the file name created in step 2nd.

However, it seems to feel more trouble, can you take these several steps to automate. It's convenient to deploy on a new machine, and more like a-_^. Of course there is, remember our makefile file that records the dependencies between the code files. Oh, we are here to use it.
Add a flag to the makefile file and add the following code under the flag:

Install
CP./hdz_pro/usr/sbin/
CP./hdz_service/etc/init.d/
cd/etc/init.d/
chmod +x Hdz_service
cd/etc/rc3.d/
Ln-sf.. /init.d/hdz_service./s99hdz_service

In the sign install without a word, the front must leave blank, this is not only conducive to reading, but also a requirement, makefile file requires a blank (space or tab) before each executable sentence.
The Hdz_pro and Hdz_service in the above code are executable file names and service names, and the names are replaced by your own, with the following commands:
Make install
Run it all OK, easy.

When people look at the things I write, I sometimes come across something like "HDZ", which is the abbreviation of my name, which means that it is to be replaced.

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.