Centos7 custom systemctl service script

Centos7 service systemctl script generally stored in: / usr / lib / systemd, there are user directory and system points

    • / usr / lib / systemd / System    # System Services, will be able to run the boot program you do not need to log in (the equivalent of boot from Kai)
    • / usr / lib / systemd / the User        # of users, in order to run the program need to log in

  Directory and there are two types of files:

    • * .Service # file service unit
    • * .Target # boot-level unit

  CentOS7 each service ending .service, usually divided into three parts: [Unit], [Service] and [Install]

[root @ localhost System] # cd / usr / lib / systemd / System
[root @ localhost System] # CAT httpd.service
[Unit] # mainly service description
Description = The Apache HTTP Server # brief description of the service
After = network.target remote-fs.target nss-lookup.target # description service Type, indicates that this service requires the network service is started after the start
Documentation = man: the httpd (. 8)
Documentation = man: apachectl (. 8)

[-Service]
the Type # = Notify core region
EnvironmentFile = / etc / sysconfig / httpd specified configuration file, and the number of conjunctions used in combination, to avoid abnormal configuration file does not exist.
ExecStart = / usr / sbin / httpd command to execute when the OPTIONS -DFOREGROUND # start the service $
ExecReload = / usr / sbin / httpd command to execute when -k graceful # restart the OPTIONS services $
ExecStop = / bin / $ {MAINPID the kill -WINCH } command to execute when # out of service
# We want systemd to give httpd some time to Finish gracefully, But Still want
# IT to the kill httpd the After TimeoutStopSec IF something Wentworth Wrong during at the
# Use the graceful sTOP. Normally, systemd SENDS SIGTERM Signal right the After at the
ExecStop #, Which Would the kill httpd. We are sending Useless to give SIGCONT here Wallpaper
# httpd Time to Finish.
KillSignal = SIGCONT
PrivateTmp = # to true representation services to independent distribution of temporary space

[Install]
WantedBy=multi-user.target # 多用户

Field Description:                              

Type types are: 
    the Simple (default): # ExecStart field to start the process of the main process 
    forking: # ExecStart field to fork () way to start, then the parent process exits, the child process will become the main process (running in the background). Usually set to forking 
    OneShot: # similar to the simple, but only once, systemd will wait for it to complete execution before launching other services 
    dbus: # similar to the simple, but will wait D - start after Bus signals 
    notify: # similar to the simple , after the start signal will be notified, and then restart other services systemd 
    idle: # similar to the simple, but to wait until other tasks are executed, will start the service. 
    
EnvironmentFile: 
    specify the configuration file, and use a combination of conjunctions number, to avoid abnormal configuration file does not exist. 

Environment: 
    followed by the shell of a plurality of different variables. 
    For example: 
    Environment = data_dir = / Data / Elk 
    Environment =. LOG_DIR = / var / log / elasticsearch 
    Environment = PID_DIR = / var / RUN / elasticsearch 
    EnvironmentFile= - / etc / sysconfig / elasticsearch 
    
conjunctions number ( - ): Before all the startup settings, add variable field, you can add conjunctions numbers 
    denote error suppression, ie when an error occurs, does not affect the execution of other commands. 
    EnviromentFile such as ` = - / etc / sysconfig / xxx` said that even if the file does not exist, it will not throw an exception 
    
KillMode type: 
    Control - Group (default): # current control group in all sub-processes will be killed 
    process: # kill only the main process 
    mixed: # master process will receive the SIGTERM signal, the child received SIGKILL signal 
    none: # process will not be killed, but the execution of the service stop command 
Restart type: 
    nO (default): exit # after no operation 
    ON - Success: # only when normal exit (exit status 0), before restarting 
    ON - failure: # the non-normal exit, restart, termination, and comprising a timeout signal 
    ON - ABORT: # only upon receipt of the termination signal is not captured, will restart abnormal: # signal is only termination or timeout will restart
    ON -
    ON - Watchdog: # timeout exit, will restart 
    always: # quit no matter what the reason, will restart 
    # for the daemon, recommended ON - failure 
RestartSec field: 
    Before expressed systemd restart the service, the number of seconds to wait: RestartSec: 30  
    
various Exec * fields: 
    # Exec * followed by the command, only to accept the "command parameters .." format, can not accept <> | & other special characters, a lot of bash syntax is not supported. If you want to support bash syntax, you need to set Tyep = OneShot 
    ExecStart: command executed when # start the service 
    ExecReload: command to execute when # restart the service 
    ExecStop: command to execute when out of service # 
    ExecStartPre: command executed before # start the service 
    ExecStartPost: # after the command to start the service performed 
    ExecStopPost: # command to be executed after stopping the service 

    
WantedBy field: 
    multi -user.target: # represents a multi-user command line mode, this setting is important 
    graphical.target: # represents a graphical user like body, it depends on the Multi -user.target
Vim / usr / lib / systemd / System / xxx.service 
[Unit] # the service description is primarily 
the Description = Test # brief description of the service 
the After = network.target # Description Service Type, indicates that this service requires the network service is started after the start 
Before = xxx.service # expressed the need for certain services start before start, After and before field involves only boot sequence does not involve dependencies. 

[Service] # core area 
Type = forking # represents the background mode. 
The User = user # Set the service to run user 
Group = User Groups user # Set the service to run 
KillMode = Control- Group systemd # define how to stop the service 
the PidFile = / usr / local / the Test / test.pid store PID # absolute path to 
the Restart =After no # define service process exits, systemd restart mode, the default is not to restart 
ExecStart = / usr / local / the Test / bin / the Startup. SH     # service start command, the command requires absolute path 
PrivateTmp = to true                                # indicate to the service distribution independent temporary space 
   
[the Install]    
WantedBy = multi-user.target # multiuser

Nginx script writing service                      

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecStop=/usr/local/nginx/sbin/nginx -s stop -c /usr/local/nginx/conf/nginx.conf
ExecReload= /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
PrivateTmp=ture
[Install]
WantedBy=multi-user.target

Zabbix_server script writing service                  

vim / usr / lib / systemd / System / zabbix_agentd.service 
# added to serve in the name of the service file 
[Unit] 
Desciption = zabbix_agentd - zabbix Monitor Client 
the After = network.target 
# started after the network boot 
#Before = xxx 
# the After Before there is no dependency, just boot sequence 
 
[service] 
the User = zabbix 
Group = zabbix 
Type = forking 
# this service runs to forking mode 
the PidFile = / tmp / zabbix_agentd.pid 
# PID file location 
ExecStartPre = / usr / bin / RM - f / tmp / zabbix_agentd.pid 
delete PID file before starting #
ExecStart = / usr / local / zabbix_agent- 3.4 . 15 / sbin / zabbix_agentd 
# startup command 
ExecReload = / bin / the kill - S HUP $ MAINPID 
# overloaded execute commands 
KillSignal = SIGQUIT 
TimeoutStopSec = 5 
# stop timeout, if not specified stop time within, forces terminated by SIGKILL 
KillMode = Mixed 
# systemd way to stop the service 
the restart = ON- failure 
after the restart # service does not exit normally 
#PrivateTmp = to true 
# indicate to the service independent distribution of temporary space 
 
[the Install] 
WantedBy = Multi- user.target 
# multi-user mode

Zabbix_agentd.service script writing service

Guess you like

Origin www.cnblogs.com/liujunjun/p/12036149.html