Ubuntu 18.04 service registration method

systemd is now used for user sessions. System sessions had already been provided by systemd in previous Ubuntu releases.

Ubuntu-18.04 with systemctl command to replace the function of service and chkconfig.
For example, before the start mysql service, the command is:

service mysql start

In order Ubuntu-18.04 start mysql services are:

systemctl start mysqld.service

systemd default read configuration files in / etc / systemd / system, file links in the directory / lib / systemd / system / files under. Execute ls / lib / systemd / system you can see there are many startup scripts, among them rc.local.service we need.
Open rc.local.service script content as follows:

[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
ExecStop=/etc/rc.local stop
ExecReload=/etc/rc.local restart
TimeoutSec=0
RemainAfterExit=yes

Composition startup scripts

General startup script consists of three parts:
[Unit] section: The main explanation for this service, including Description and After. Description is used to describe the service, After a description of the service category.

[Service] section: Set some specific operating parameters of the service.
Type = forking is in the form of running in the background,
the User = the Users user set the service to run,
Group = the Users is to set the service to run user group,
the PidFile file path to store the PID,
ExecStart for the specific operational command services,
ExecReload for the resumption command ,
ExecStop stop command,
PrivateTmp = True representation to the service allocates a separate temporary space
Note: [service] section of the start, restart, stop all command must be an absolute path, using a relative path will be given!

[Install] section: Service installation settings, namely how to do boot. It can be set to multiple users.

Start-up service registration step

  1. As can be seen from the script, the startup sequence / etc / rc.local was behind the network, but it is less clear that the Install section, there is no definition of how to do the boot, it is clear that this configuration is invalid. So we need to help him followed by the [Install] section:

[Install]
WantedBy=multi-user.target

  1. Need to look at, ubuntu-18.04 is no default /etc/rc.local this file, you need to create your own. The script needs to start in /etc/rc.local in here as a test, just put in some test scripts.
touch /etc/rc.local   //创建rc.local
vi /etc/rc.local
		//启动脚本的内容
		#!/bin/sh -e
		echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
        exit 0
  • Execute permissions to modify the startup script
  • chmod +x /etc/rc.local
    
    1. Done this step, you also need the final step in front of us that systemd default read / etc configuration files in / systemd / system, so it needs to create a soft link in / etc / systemd / system directory
    ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/ 
    
    1. Next, reboot the system and see if the file exists /usr/local/text.log know whether the boot script into effect.

    appendix

    To rc.local.service as an example, the command part systemctl

    1. From the perspective of service
    systemctl start rc.local.service    #启动服务
    systemctl stop rc.local.service    #停止服务
    systemctl restart rc.local.service    #重启服务
    systemctl status rc.local.service    #查看服务当前状态
    systemctl is-active rc.local.service    #查看服务是否处于启动状态
    systemctl daemon-reload    //修改脚本后重新加载
    
  • From a system perspective
  • systemctl enabled rc.local.service    #设置为开机自动启动
    systemctl disabled rc.local.service    #设置为开机禁用
    systemctl list-units --type=service    #显示所有已经启动的服务
    

    About service file of some options, reference
    http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

    Released four original articles · won praise 8 · views 9120

    Guess you like

    Origin blog.csdn.net/u012254599/article/details/103241998