centos 7 Used in systemd Control system services , Simpler , Previously required to start service service xxxx
start|stop etc. . What we need now is systemctl start|stop xxx. For example, the startup of my configuration tomcat,systemctl start
tomcat. Very convenient . It used to be , Now directly in /usr/lib/systemd/system Create a new one inside tomcat.service, The content is as follows :


[Unit] Description=tomcatapi After=network.target [Service] Type=forking
ExecStart=/usr/local/soft/tomcat/tomcat8/bin/startup.sh ExecReload=
ExecStop=/usr/local/soft/tomcat/tomcat8/bin/shutdown.sh PrivateTmp=true
[Install] WantedBy=multi-user.target
And give this tomcat.service file chomod +x Permission is enough ! Finally, under restart systemctl, The command is as follows :systemctl
daemon-reload
Finally, we can pass systemctl start tomcat It's on !

above tomcat.service Need to change on demand . The paths are different .

Here is the reprint :



The settings are divided into three parts

*
[Unit]: unit Description of itself , And other dependencies daemon Settings for , Include services before starting this unit Settings like ;

*
[Service], [Socket], [Timer], [Mount], [Path]..: different unit type
You have to use the corresponding settings . We have sshd.service To be a model , So we use it here [Service] To set .
In this project, the script is mainly used to standardize service startup , Environment profile filename , How to restart and so on .

*
[Install]: This is the project unit To which target The meaning of going inside !

As for some setting rules in the configuration file, please explain them :

*
Setup items are usually repeatable , For example, I can repeat two settings After In the configuration file , however , The later settings will replace the earlier ones ! therefore , If you want to reset the setting to zero , Similar to “
After= ” Settings for , That is, there is nothing after the equal sign of the project , It's time to set it to zero (reset).

*
If the setting parameter needs to have “ yes / no ” Project of ( Brin value , boolean), You can use 1, yes, true, on Start on behalf of , use 0, no, false,
off Closed on behalf of ! Whatever you like !

*
Blank line , Starts with # or ; That line of , All for comments !

There are many settings in each part , Let's use a simple table to illustrate each project !

[Unit] part  
Set parameters Parameter meaning description
Description When we use systemctl list-units Time , Simple instructions that will be output to the administrator ! of course , use systemctl
status Description of the exported service , It's the same project !
Documentation This project provides the administrator with the function of further file query ! The file provided can be the following data :
Documentation=http://www.... Documentation=man:sshd(8) 
Documentation=file:/etc/ssh/sshd_config
After Explain this unit Where is it daemon Start after start ! Basically, it just describes the order in which the services are started , There is no mandatory requirement that the services in the
unit To start . with sshd.service For example , The document refers to After In the back network.target as well as
sshd-keygen.service, But if these two unit Forced start without start sshd.service The words , that sshd.service
It should still be able to start ! It's about Requires There are differences in the settings of !
Before And After It means the opposite , What is the best way to start a service before it starts . However, this is only to regulate the order of service startup , Not mandatory .
Requires Clearly define this unit Where do I need to be daemon Start after start ! It's about setting up dependent services ! If the leading service for this setting is not started , So this unit
It won 't start !
Wants And Requires be just reversed , This is the norm unit
It's better to start what service is better after that ! however , There is no clear standard ! The main purpose is to create an environment for users to operate better . therefore , this Wants
If the following service is not started , It's not going to affect that unit itself !
Conflicts Services representing conflicts ! That is to say, if the service followed by this project is started , So this unit Can't start by itself ! We unit
With startup , The service after this project cannot be started ! It's a conflict check anyway !
Let's take a look at [Service] Which items can be used !

[Service] part  
Set parameters Parameter meaning description
Type Explain this daemon How to start , Will affect ExecStart oh ! generally speaking , There are several types simple: Default , this daemon Mainly by
ExecStart Command string to start , Resident in memory after startup .forking: from ExecStart Started program passed spawns Extend other subroutines as this
daemon Main services of . The native parent program will stop running after startup . conventional unit Most services belong to this kind of project , for example httpd this WWW service , When httpd
Because it's running too long, it's about to end , be systemd Will regenerate another subroutine and continue to run , Delete the parent program again . It is said that this kind of performance is better !!oneshot: And
simple similar , But it's over when it's done , Not resident in memory .dbus: And simple similar , But this daemon You have to get a D-Bus
After the name of , Will continue to run ! So when setting this project , It's usually set BusName= Only then !idle: And simple similar , intend , To do this daemon
All work must be carried out smoothly before it can be carried out . This kind of daemon It is usually a service that can only be executed after the power is turned on ! The most important project is probably simple, forking And
oneshot 了 ! After all, many services need subroutines (forking), There are more actions that need to be performed only once when the machine is turned on (oneshot), For example, check and mount the file system, etc .
EnvironmentFile You can specify the environment profile for the startup script ! for example sshd.service Configuration files written to /etc/sysconfig/sshd
among ! You can also use Environment= There are many different ones at the back Shell Variable to give settings !
ExecStart It's actually doing this daemon Instructions or scripts for . You can also use ExecStartPre ( before ) as well as ExecStartPost
( after ) Two setup items to , Perform additional command actions . But what you have to pay special attention to , Command string accept only “ instructions parameter parameter ...” Format of , inadmissibility <, >,
>>, |, & Etc , a great deal of bash Grammar is not supported !
therefore , To use these special characters , It's better to write it directly into the instruction script ! however , The above grammar is not completely useless , or , To support more complete bash grammar , Then you have to use
Type=oneshot That's it ! other Type Can't support these characters .
ExecStop And systemctl stop Implementation of , Instructions to shut down this service .
ExecReload And systemctl reload Relevant instruction behavior
Restart When setting Restart=1 Time , Then when daemon After service termination , Will start this service again . for instance , If you are tty2
Log in using the text interface , Log out after operation , Basically , This time tty2 It's the end of the service . But you'll see a new one on the screen right away tty2 Waiting for your login ! That is
Restart Function of ! Unless used systemctl Force this service to shut down , Otherwise, this service will continue to be generated repeatedly !
RemainAfterExit When set to RemainAfterExit=1 Time , When this daemon
After all programs are terminated , This service will try to start again . This is true for Type=oneshot The service is very helpful !
TimeoutSec If the service is started or shut down , It's not going well for some reason “ Normal start or end ” In the case of , How long do we have to wait to get in “ Force end ” Status of !
KillMode Can be process, control-group, none One of , If it is process be daemon
On termination , Only major programs will be terminated (ExecStart The next series of instructions ), If it is control-group Time , So daemon Others generated
control-group Procedure of , It's going to be shut down . If it is none The words , No program will be closed !
RestartSec And Restart It's kind of relevant , If this service is shut down , When a reboot is needed , Probably sleep How long does it take to restart . The default is
100ms ( millisecond ).
last , Let's see that again Install What other items are available in ?

[Install] part  
Set parameters Parameter meaning description
WantedBy Most of the settings are followed by *.target unit ! intend , this unit Which one is attached target unit
Below ! generally speaking , Most services unit It's all attached multi-user.target below !
Also When this unit By itself enable Time ,Also Followed by unit Please enable Meaning of ! That is, services with dependencies can be written here !
Alias The meaning of a link alias ! When systemctl enable Relevant services , The service will create the linked file ! with
multi-user.target take as an example , This guy is used as the default operating environment default.target Planning of , So when you set up
default.target Time , this /etc/systemd/system/default.target Will be linked to
/usr/lib/systemd/system/multi-user.target Luo !