Linux服务管理工具systemctl与service

一、systemctl 介绍

1.1 systemd服务简介

systemd 是一个系统和服务管理器,它是Linux系统中一套基本的构建模块,提供了一系列工具的集合,用于管理后台服务、状态查询、日志归档、设备管理、电源管理和定时任务等许多功能。

systemd 作为PID为1的进程(后台服务),是系统中所有其它进程的父进程。

systemd通过单元文件(Unit files,具有.service后缀)来描述和管理不同的系统资源和服务。Systemd 支持的 12 种 Unit 文件类型。

1.2 systemctl 简介

systemctl 是一个命令行工具,用于与systemd进行交互。通过systemctl,用户可以启动、停止、重启、查看状态以及管理系统中的服务单元。

systemd作为后台服务运行,而systemctl则提供了用户与systemd交互的接口。用户通过systemctl发送指令,systemd执行相应的操作。

systemctl命令实际上是向systemd发送请求,告诉systemd要做什么。

二、service 文件

2.1 service 文件简介

service 文件是为 定义软件服务的文件(systemd服务单元文件,具有.service后缀),这些文件通常位于以下目录:

1
2
3
4
5
6
# 系统或用户自定义的配置文件
/etc/systemd/system/
# 软件运行时生成的配置文件
/run/systemd/system
# 系统或第三方软件安装时添加的配置文件。
/usr/lib/systemd/system

Systemd 默认从 /etc/systemd/system/ 目录下读取配置文件。但是,里面存放的大部分文件都是符号链接都指向 /usr/lib/systemd/system/目录,真正的配置文件存放在这个目录。

service文件主要三大模块

1
2
3
4
5
[Unit]:    # 定义与Unit类型无关的通用选项,用于提供unit的描述信息、unit行为及依赖关系等

[Service]: # 定义如何启动、停止、重启当前服务。

[Install]: # 定义如何安装这个配置文件,即怎样做到开机启动。

2.2 Unit部分

1
2
3
4
5
Description:        # 对服务的简单描述
After:              # 在哪些服务之后启动
Before:             # 在哪些服务器启动之前启动
Requires:           # 可以指定服务依赖于哪些服务(强依赖)
Wants:              # 可以指定服务依赖于哪些服务(弱依赖)

2.3 Service部分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
EnvironmentFile:   # 指定当前服务启动的环境变量
ExecStartPre:      # 指定服务启动前执行的命令或脚本
ExecStart:         # 指定服务启动时执行的命令或脚本
ExecStartPost:     # 指定服务启动后执行的命令或脚本
ExecStop:           # 指明停止服务要运行的命令或脚本
RestartSec:         # 指定服务在重启时等待的时间,单位为秒
ExecReload:         # 指明重启服务要运行的命令或脚本
Restart:            # 重启设置
KillMode:           # 指定停止的方式
Restart:            # 指定重启时的类型
Type:               # 指定启动类型,

其中, type的可选值:

1
2
3
4
5
simple      # 指定ExecStart字段的进程为主进程
forking     # 指定以fork() 子进程执行ExecStart字段的进程
oneshot     # 执行一次
notify      # 启动后会发送通知信号通知systemd
idle        # 等其它任务结束后才运行

Restart 可选值:

1
2
3
4
5
6
no:            # 退出后不会重启
on-success:    # 当进程正常退出时(退出码为0) 执行重启
on-failure:    # 当进程不正常退出时(退出码不为0) 执行重启
on-abnormal:   # 当被信号终止和超时执行重启on-abort:      当收到没有捕捉到的信号终止时执行重启
on-watchdog:   # 当看门狗超时时执行重启
always:        # 一直重启

KillMode可选值:

1
2
3
4
control-group: # 杀掉当前进程中所有的进程
process:       # 杀掉当前进程的主进程
mixed:          # 主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
none:           # 不杀掉任何进程

2.4 Install部分

1
2
3
4
5
Alias:         # 别名,可使用systemctl command Alias.service
RequiredBy:    # 被哪些units所依赖,强依赖
WantedBy:      # 被哪些units所依赖,弱依赖
Also:          # 安装本服务的时候还要安装别的相关服务
Install:       # 一般填为WantedBy=multi-user.target

2.5 service文件样例

  1. 将部署的nginx注册为系统服务:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
#启动检测命令
ExecStartPre=/data/nginx/sbin/nginx -t
#启动命令
ExecStart=/data/nginx/sbin/nginx -c /etc/nginx/nginx.conf
#重载配置文件命令
ExecReload=/data/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/data/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target
  1. 将部署的mysql注册为系统服务
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target 

[Service]
Type=forking
#指定PID文件
PIDFile=/data/mysql/data/centos-linux.shared.pid
#启动MySQL
ExecStart=/data/mysql/support-files/mysql.server start
#重载
ExecReload=/bin/kill -s HUP $MAINPID
#停止服务
ExecStop=/data/mysql/support-files/mysql.server 
stopPrivateTmp=false 
[Install]
WantedBy=multi-user.target

三、systemctl命令合集

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#启动服务
systemctl start name.service
#停止服务
systemctl stop name.service
#重启服务
systemctl restart name.service
#查看服务状态
systemctl status name.service
#禁止自动和手动启动
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态
systemctl is-active name.service
#查看所有已经激活的服务
systemctl list-units --type|-t service
#查看所有服务
systemctl list-units --type service --all
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list namels /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#查看服务的依赖关系
systemctl list-dependencies name.service
#杀掉进程
systemctl kill unitname
#重新加载配置文件
systemctl daemon-reload
#关机
systemctl halt
#重启服务器
systemctl reboot