Centos systemctl 说明
启动脚本目录,一般放在 /usr/lib/systemd/system
或者 /etc/systemd/system
下面是 nginx 启动文件
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
- Description 给出当前服务的简单描述,
- After 启动顺序,不涉及依赖关系,设置依赖关系,需要使用
Wants
和Requires
- Wants 如
Wants=remote-fs.target nss-lookup.target
,表示emote-fs.target nss-lookup.target
存在"弱依赖"关系,即如果emote-fs.target
启动失败或停止运行,不影响nss-lookup.target
继续执行。 - Requires 字段则表示"强依赖"关系,即如果该服务启动失败或异常退出,那么其它也必须退出
Wants
与Requires
只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。
[Service]
-
EnvironmentFile 指定当前服务的环境参数文件。该文件内部的key=value键值对,可以用$key的形式,在当前配置文件中获取。
-
ExecStart :定义启动进程时执行的命令
-
ExecReload :重启服务时执行的命令
-
ExecStop :停止服务时执行的命令
-
ExecStartPre :启动服务之前执行的命令
-
ExecStartPost :启动服务之后执行的命令
-
ExecStopPost :停止服务之后执行的命令
1.如果定义了EnvironmentFile,执行命令可以加载EnvironmentFile字段指定的环境参数,注意这里需要使用绝对路径
2.所有的启动设置之前,都可以在命令之前加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如,ExecReload=-/bin/kill -s HUP $MAINPID
-
Type字段定义启动类型
1 . simple(默认值):ExecStart 启动的进程为主进程
2 . forking:ExecStart 将以fork()方式启动,此时父进程将会退出,子进程将成为主进程
3 . oneshot:类simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
4 . dbus:类simple,但会等待 D-Bus 信号后启动
5 . notify:类simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
6 . idle:类simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合 -
TimeoutStopSec 设定该服务允许的最大停止时长。如果该服务未能在限定的时长内成功停止,那么将会被强制使用 SIGTERM 信号关闭
-
RemainAfterExit 表示进程退出以后,服务仍然保持执行 (yes/no)
-
KillMode 如何停止服务。
1.process 表示只停止主进程,子进程保持
2.control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
3.mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
4.none:没有进程会被杀掉,只是执行服务的 stop 命令。 -
Restart 重启方式
1.on-failure 非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
2.on-success:只有正常退出时(退出状态码为0),才会重启
3.on-abnormal:只有被信号终止和超时,才会重启
4.on-abort:只有在收到没有捕捉到的信号终止时,才会重启
5.on-watchdog:超时退出,才会重启
6.no(默认值):退出后不会重启
7.always:不管是什么退出原因,总是重启
对于守护进程,推荐设为on-failure,对于那些允许发生错误退出的服务,可以设为on-abnormal。
- RestartSec字段:表示重启服务之前,需要等待的秒数
[Install]
- WantedBy 表示该服务所在的 Target
Target
的含义是服务组,表示一组服务。WantedBy=multi-user.target
指的是,服务所在的 Target 是multi-user.target
。
这个设置非常重要,因为执行systemctl enable nginx.service
命令时,nginx.service
的一个符号链接,就会放在/etc/systemd/system
目录下面的multi-user.target.wants
子目录之中。
使用命令systemctl get-default
可以查看默认启动的Target,在这个组里的所有服务,都将开机启动
[root@972]# systemctl get-default
multi-user.target
使用命令
systemctl list-dependencies multi-user.target
可以查看 multi-user.target 包含的所有服务
Target 也有自己的配置文件,
[root@972]# systemctl cat multi-user.target
# /lib/systemd/system/multi-user.target
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
- Requires 要求
basic.target
一起运行。 - Conflicts 如果
rescue.service
或rescue.target
正在运行,multi-user.target
就不能运行,反之亦然。 - After 表示
multi-user.target
在basic.target
、rescue.service
、rescue.target
之后启动,如果它们有启动的话。 - AllowIsolate 是否允许使用
systemctl isolate
命令切换到multi-user.target
。
systemctl isolate shutdown.target
状态
systemctl start
命令可以查看服务的状态
[root@972]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
# 配置文件的位置,是否设为开机启动
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
#表示正在运行
Active: active (running) since Mon 2020-11-16 17:11:40 CST; 20h ago
#主进程ID 21286
Main PID: 21286 (nginx)
# 应用的所有子进程
CGroup: /system.slice/nginx.service
├─21286 nginx: master process /usr/sbin/nginx
├─21287 nginx: worker process
└─21288 nginx: worker process
#应用的日志
Nov 16 17:11:40 972 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 16 17:11:40 972 nginx[21280]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 16 17:11:40 972 nginx[21280]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 16 17:11:40 972 systemd[1]: Started The nginx HTTP and reverse proxy server.