In linux, the executable program is made into a service.

Source: Internet
Author: User
In linux, the executable program is made into a service-general Linux technology-Linux programming and kernel information. The following is a detailed description. When the linux system is started, we can see that many service programs are started one by one (that is, the lines with blue [OK ), these programs run in the background play an important role in using computers. The programs run in this way have the following features:

Start and shut down (nonsense! Is it still running after shutdown ?)
Background running
Use service commands to control or view the running status of the corresponding program

Well, that's good. How can we make our own program into such a service?

It consists of the following three steps:

1. Place the executable program in a place that can be found in a linux system.

Enter env in the linux Command Prompt, and press enter to see the environment variable. There is a PATH in it. Right, the directory specified in this item is the one that can be found by the system, put our executable program in one of the directories. We usually put it in the/usr/sbin/directory (where there is something to put, it is better not to put it in disorder ).

2. In the/etc/init. d/directory, create a file named "service.

If we open the directory/etc/init. d/. The files you see are actually service program files. The content of each file is similar. We can see that the files here are basically the same in the file structure. Almost every file has a mark such as start, stop, restart, and status, I just need to replace the corresponding executable program with our own executable program in these marks. Do you know how to do it? That is: Copy, modify, and save.

3. In the/etc/rc3.d/directory, create a symbolic connection file pointing to the service file created in step 1.

Open the/etc/rc3.d/directory, and we will see that all these are symbolic Connection Files (the symbolic connection in linux is equivalent to a shortcut in windows ). Use the following command format:

Ln-sf target file name connection file name

The format of "SXX target file name" is recommended for connecting file names. XX is generally an integer from 1 to 100, which indicates the startup priority. The larger the number, the lower the priority. For example: service A depends on service B, and the XX number of service A should be greater than that of Service B. The "target file name" is followed by a symbolic connection to the desired file at a glance.

Directory/etc/rc3.d/is the directory automatically searched when the system starts. The target files of the symbolic connection files under the directory will be run, this is the reason for establishing a symbolic connection in this directory-to run on startup.

Now, after completing the three steps above, you can make one of your executable programs into a service of the system. It has some specific features we mentioned at the beginning. 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 above hdz_service is the service name, that is, the file name created in step 1.

However, it seems to be troublesome. Can you automate these steps? In this way, it is easy to deploy on a new machine, and it is more like-_ ^! Of course there is a solution. Do you still remember the makefile that records the dependencies between code files? We will use it here.

Add a flag to the makefile file and add code similar to the following 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

There is no sentence under the install flag. It must be left blank before, which is not only conducive to reading, but also a requirement, makefile requires that each executable statement must be blank (space or tab ).

In the above Code, hdz_pro and hdz_service are executable file names and service names respectively. You can replace these two names with your own. Use the following command to perform the operation:

Make install

After running, everything is OK. Is that simple?

When reading what I wrote, people may encounter something like "hdz" from time to time. This is the abbreviation of my name, indicating that this is exactly what I want to replace.
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.