centos 7 Adopted insystemd Control system services, More simple, Previously required to start service service xxxx
start|stop etc.. What we need now issystemctl start|stop xxx. For example, the startup of my configurationtomcat,systemctl start
tomcat. Very convenient. Used to be, Now directly in/usr/lib/systemd/system Create a new one insidetomcat.service, The contents are 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 thistomcat.service filechomod +x Permission can be! Finally, under restartsystemctl, The order is as follows:systemctl
Finally, we can passsystemctl start tomcat Start up!

Abovetomcat.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 Set up, 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] Set up.
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= ” Set up, 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” Item ( Brin value, boolean), You can use 1, yes, true, on Representative start, use 0, no, false,
off Representative closes! Whatever you like!

Blank line, Begin with # or ; That line, All for comments!

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

[Unit] part  
Setting 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) 
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 Ability to start. with sshd.service For example, The document refers to After Behind there are network.target as well as
sshd-keygen.service, But if these two unit Forced start without start sshd.service Words, that sshd.service
It should still be able to start! This and 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, Then this unit
Will not be activated!
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
Start up, 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  
Setting parameters Parameter meaning description
Type Explain this daemon How to start, Will affect ExecStart oh! generally speaking, There are several types simple: Default value, this daemon Mainly from
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's said that the 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
Name after, Will continue to run! So when setting this project, It's usually set BusName= Talent!idle: And simple Similar, Intend, To do this daemon
All work must be carried out smoothly before it can be carried out. Such 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, 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 all.! 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 set Restart=1 Time, Then this daemon After service termination, Will start this service again. for instance, If you are in 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! 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 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” Under the circumstances, How long do we have to wait to get in“ Mandatory termination” State!
KillMode Could be process, control-group, none One of, If it is process be daemon
At the time of termination, Only major programs will be terminated (ExecStart The next series of instructions), If it is control-group Time, Thus daemon Others generated
control-group Procedure, It's going to be shut down. If it is none 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  
Setting parameters Parameter meaning description
WantedBy Most of the settings are followed by *.target unit ! Intend, this unit Which one is attached target unit
Underneath! generally speaking, Most services unit It's all attached multi-user.target Below!
Also When this unit Itself being enable Time,Also Subsequent connection unit Also please enable Meaning! 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, 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!