Linux

Centos systemctl 说明

2020-11-17  本文已影响0人  ___n

启动脚本目录,一般放在 /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]

WantsRequires 只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。

[Service]

1.如果定义了EnvironmentFile,执行命令可以加载EnvironmentFile字段指定的环境参数,注意这里需要使用绝对路径
2.所有的启动设置之前,都可以在命令之前加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如,ExecReload=-/bin/kill -s HUP $MAINPID

对于守护进程,推荐设为on-failure,对于那些允许发生错误退出的服务,可以设为on-abnormal。

[Install]

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

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.
上一篇下一篇

猜你喜欢

热点阅读