Linux centos7 new system services systemctl

Linux centos7 new system services systemctl

  1. Written service you want to run, can be a python, shell, c, etc. language to write programs. I have written here is of a python.

    

    This is what I want it to turn into a python program system services.

  2. Preparation of service documents

    We look at / usr / lib / systemd / system / directory the following files:

    

 

 

      Here you can see a lot of service to save the file, just cut the part. We need to write the following in this file is the service catalog. We recommend the use of the name of the program file named:

    

    Content writing:

    

 

 

     

Content of parameters:

Settings are divided into three parts

[Unit]: unit description itself, as well as with other dependencies daemon settings, including settings before starting this unit and the like after what services;

[Service], [Socket], [Timer], [Mount], [Path] ..: to have a different unit type using the corresponding setting items. We take that as a template to sshd.service, so here on the use of [Service] to set. Within this project, mainly in the service specification script starts, environment configuration file name, restart the way and so on.

[Install]: This project is to install this unit to which target to go inside mean!

As for some settings within the configuration file rules still have to explain:

Setting items can usually be repeated, for example, I can set two repeat After the configuration file, but the latter setting will replace the previous Oh! So, if you want to set the value to zero, you can use something like "After =" setting, that is after the equal sign of the project's nothing, it is the setting of the zero (reset).

If the project parameters need to have "Yes / No" (boolean, boolean) is set, you can use 1, yes, true, on behalf start with 0, no, false, off behalf shut down! Choose as you like Hello!

Blank line that begins with # or; of the line, represents comment!

Each part of the set there is also a lot of small items, we use a simple table to explain each item good!

 

[Unit] part   

Setting Parameter Description significance

Description is that when we use systemctl list-units, the administrator will be output to the easy instructions to see! Of course, the use of this service systemctl status output, and it is this project!

Documentation of this project to provide administrators to further functional file queries! Files can be provided the following data: Documentation = http: // www .... Documentation = man: sshd (8) Documentation = file: / etc / ssh / sshd_config

After a description of this unit after which the daemon is started before the start of meaning! Basically only the service order to start of illustration only and not mandatory inside the service must be started after this unit to start. Sshd.service of content-for example, the document referred back After have network.target and sshd-keygen.service, but if these two unit does not start and forced to start sshd.service, then sshd.service should still be able to start of! This Requires the setting is different Oh!

Before and After the opposite sense, it is what the service starts before the best start this service mean. But this is only the start of the service order specification, meaning not mandatory.

It requires a clear definition of this unit needs to be able to start until after which the daemon to start! It is to set the service dependency! If the leading service in this setting is not enabled, then the unit will not be started!

Wants to Requires contrary, specification of what the service after the unit may also want to start a better meaning! However, there is no clear standard it wants! The main purpose is to allow users to create a better operating environment. Therefore, take this back Wants services If it does not, in fact, will not affect the unit itself!

Conflicts representatives of the conflicting service! That is behind this project then start the service if there is, then we can not start the unit itself! Our unit has started the service after the project will not start! Anyway, that is a conflict of checking it!

Then look at which projects can be used in [Service] them!

 

[Service] part     

Setting Parameter Description significance

Type Description start the daemon way affect ExecStart Oh! In general, there are several simple types of the following: default value, this daemon mainly ExecStart contact instruction sequence to start, after starting up the resident in memory. forking: start by extending through ExecStart program spawns another subroutine as a main service of this daemon. Native parent program will terminate after the started running. The traditional unit serving mostly belong to this project, such as the WWW service httpd, httpd when the program is running because for too long and therefore is about to end, and then re-birth systemd will continue to run after another subroutine, and then delete the parent program. It is said that this performance is better! ! oneshot: simple and similar, but this program ended after the work is completed, it will not be resident in memory. dbus: simple and similar, but this daemon must, after obtaining the name of a D-Bus, will continue to run! Therefore, set this project, usually also set BusName = job! idle: simple and similar, meaning, to be performed will be performed after this daemon must all work smoothly executed. Such a daemon is usually the last to turn to the implementation of the service! Probably more important project is simple, forking with a oneshot! After all, many services require routine (forking), and there is more action needs to be performed only once (oneshot) at boot time, such as checking and mounting file systems, etc., ah.

EnvironmentFile can specify the boot environment configuration file for the script! Such as writing sshd.service configuration file to / etc / sysconfig / sshd them! You can also use the Environment = followed by the number of different variables to give Shell set!

ExecStart this daemon is the actual execution of instructions or scripts. You can also use ExecStartPre (before) and ExecStartPost (after) two projects set to start before the actual service, additional instructions behavior. But you have to pay special attention to that command only accepts strings "command Parameter ..." format, can not accept <,>, >>, |, & other special characters, a lot of bash syntax does not support the Oh! So, when you want to use these special characters, write the best script directly to the command to go inside! However, the above grammar is not entirely can not be used, that is, to support more complete bash syntax, then you have to use Type = oneshot job Oh! Other Type did not support these characters.

Systemctl stop execution ExecStop and related instructions carried out by closing the service.

ExecReload and systemctl reload instructions relating to behavior

Restart When set Restart = 1, then this daemon after termination of service, the service will start again. For example, if you use the text interface tty2 login, logout operation is completed, basically, this time tty2 has ended the service. But you'll see the screen at once and generate a new login screen tty2 waiting for your landing! That's Restart function! Unless systemctl forcibly turned it off, otherwise the service will generate an endless supply of repeated!

RemainAfterExit When set to 1 RemainAfterExit =, then when all this daemon program belongs to terminate, this service will try to start again. This Type = oneshot helpful service!

TimeoutSec If this service is started or shut down because of some reason can not successfully lead to the case of "normal startup or normal end", then how long before we enter "to force the end of the" state!

KillMode may process, control-group, none of the one, if the daemon process is terminated, the main program will terminate (ExecStart contact string of instructions later), if a control-group, then thereby daemon other control-group is generated by the program, it will also be closed. If none, then the program will not be closed Oh!

RestartSec and Restart little relevance, if this service is turned off and then restarted when needed, about how much time you want to restart the meaning of sleep. The default is 100ms (milliseconds).

[Install] part       

Setting Parameter Description significance

WantedBy this setting take back most of * .target unit! Means that this unit is attached is itself linked to a target unit in which the following! In general, unit nature of the service are mostly attached hanging in multi-user.target below!

Also, when the unit itself is currently enable, Also back then the unit please enable mean! That is, the service has a dependency can be written here yet!

Alias ​​alias be a link to the meaning!

 

Description description, in fact, really feel useless, except you can see below.

 

 

 

After: This is an automatically mount my external storage service, you need to be after the network services and iscsi service together, we should pay attention to here, may not be pressing explain write, you get to see their own services, other pressing his need to write on OK.

3. Complete

After that, you can use systemctl order to control this procedure.

You can also choose to boot from the start:

systemctl enable automount

Guess you like

Origin www.cnblogs.com/52why/p/12362871.html