Linux study notes the Centos7 custom systemctl service script

0x00 Overview

  Before working environment has been used Centos6 version, the script has been using /etc/init.d/xxx; after upgrade to Cento7, although before the startup scripts can also be used, but has not been used systemctl custom scripts.

This article summarizes the case for specific use. Centos7 boot from the first program completely replaced by systemd init start-up mode, and systemd unit rely on ways to control the boot service, power levels and other functions.

 

0x01 configuration

  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 does 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]

Copy the code
Vim / usr / lib / systemd / System / xxx.service 
[Unit]    # mainly service description 
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 the User         # set the service to run user 
Group = the User        # User Group set the service to run 
KillMode = Control-Group    # define how to stop systemd service 
the PidFile = / usr / local / the Test / test.pid     # store absolute path to PID
= NO the Restart         # defined service process and exit, systemd restart mode, the default is not to restart 
ExecStart = / usr / local / the Test / bin / startup.sh     # services start command, the command needs the absolute path 
PrivateTmp = to true                                # indicate to the service distribution independent temporary space 
   
[the Install]    
WantedBy = multi-user.target   # multiuser
Copy the code

 

0x02 Field Description

Copy the code
Type types are: 
    the Simple (default): # to start the process ExecStart field-based 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 for D-Bus signal to start 
    the 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 
    
Connective sign ( - ): Before starting all settings, the added variable field, can add conjunctions no 
    representation 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: # only kill the main process 
    Mixed:    # master process will receive the SIGTERM signal, the child received signal SIGKILL 
    none:     # no process will be killed, but the execution of the service stop command 
Restart type: 
    nO (default): #After exiting no operation 
    ON-Success:   # Only when the normal exit (exit status 0), will restart the 
    ON-failure:   # the non-normal exit, restart, termination, and comprising a timeout signal 
    ON-Abnormal: # Only termination or timeout signal, will restart the 
    oN-ABORT:     # only upon receipt of the termination signal is not captured, will restart the 
    oN-Watchdog: # timeout exit, will restart 
    Always:       # no matter what the reason for exit, will restart 
    # for daemon, recommended by failure-ON 
RestartSec fields: 
    representation before 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 parameter .. "format, can not accept <> | & other special characters, a lot of bash syntax is not supported. If you want to support Tyep = oneshot bash syntax, you need to set 
    : ExecStart     # command to execute when the service starts
    ExecReload:    # command to execute when to restart the service 
    ExecStop:      # command to execute when out of service 
    ExecStartPre: # command before starting service 
    ExecStartPost: # command to start the service after 
    ExecStopPost: # command after stop 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 multi-user.target
Copy the code

 

0x03 systemctl command

daemon-reload systemctl     # overloaded system services 
systemctl * .service enable # Set a service boot       
systemctl Start * .service   # start a service   
systemctl .service * STOP    # Stop a service 
systemctl reload * .service # restart a service

 

reference

 

0x00 Overview

  Before working environment has been used Centos6 version, the script has been using /etc/init.d/xxx; after upgrade to Cento7, although before the startup scripts can also be used, but has not been used systemctl custom scripts.

This article summarizes the case for specific use. Centos7 boot from the first program completely replaced by systemd init start-up mode, and systemd unit rely on ways to control the boot service, power levels and other functions.

 

0x01 configuration

  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 does 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]

Copy the code
Vim / usr / lib / systemd / System / xxx.service 
[Unit]    # mainly service description 
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 the User         # set the service to run user 
Group = the User        # User Group set the service to run 
KillMode = Control-Group    # define how to stop systemd service 
the PidFile = / usr / local / the Test / test.pid     # store absolute path to PID
= NO the Restart         # defined service process and exit, systemd restart mode, the default is not to restart 
ExecStart = / usr / local / the Test / bin / startup.sh     # services start command, the command needs the absolute path 
PrivateTmp = to true                                # indicate to the service distribution independent temporary space 
   
[the Install]    
WantedBy = multi-user.target   # multiuser
Copy the code

 

0x02 Field Description

Copy the code
Type types are: 
    the Simple (default): # to start the process ExecStart field-based 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 for D-Bus signal to start 
    the 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 
    
Connective sign ( - ): Before starting all settings, the added variable field, can add conjunctions no 
    representation 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: # only kill the main process 
    Mixed:    # master process will receive the SIGTERM signal, the child received signal SIGKILL 
    none:     # no process will be killed, but the execution of the service stop command 
Restart type: 
    nO (default): #After exiting no operation 
    ON-Success:   # Only when the normal exit (exit status 0), will restart the 
    ON-failure:   # the non-normal exit, restart, termination, and comprising a timeout signal 
    ON-Abnormal: # Only termination or timeout signal, will restart the 
    oN-ABORT:     # only upon receipt of the termination signal is not captured, will restart the 
    oN-Watchdog: # timeout exit, will restart 
    Always:       # no matter what the reason for exit, will restart 
    # for daemon, recommended by failure-ON 
RestartSec fields: 
    representation before 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 parameter .. "format, can not accept <> | & other special characters, a lot of bash syntax is not supported. If you want to support Tyep = oneshot bash syntax, you need to set 
    : ExecStart     # command to execute when the service starts
    ExecReload:    # command to execute when to restart the service 
    ExecStop:      # command to execute when out of service 
    ExecStartPre: # command before starting service 
    ExecStartPost: # command to start the service after 
    ExecStopPost: # command after stop 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 multi-user.target
Copy the code

 

0x03 systemctl command

daemon-reload systemctl     # overloaded system services 
systemctl * .service enable # Set a service boot       
systemctl Start * .service   # start a service   
systemctl .service * STOP    # Stop a service 
systemctl reload * .service # restart a service

 

reference

 

Guess you like

Origin www.cnblogs.com/JetpropelledSnake/p/10931323.html