一、概述
systemd是一个系统和服务管理器,systemd是Linux系统中一套基本的构建模块,提供了一系列工具的集合,用于管理后台服务、状态查询、日志归档、设备管理、电源管理和定时任务等许多职责。
systemd作为PID为1的进程,是系统中所有其他进程的父进程。
systemctl是一个命令行工具,用于与systemd进行交互。通过systemctl,用户可以启动、停止、重启、查看状态以及管理系统中的服务单元
systemd作为后台服务运行,而systemctl则提供了用户与systemd交互的接口。用户通过systemctl发送指令,systemd执行相应的操作
systemctl的命令实际上是向systemd发送请求,告诉systemd要做什么。
systemd通过单元文件(Unit files)来描述和管理不同的系统资源和服务。Systemd 支持的 12 种 Unit 文件类型。下面就将我们生产环境中常用到的Service单元文件展开讲解。
二、service文件
定义软件服务的文件通常是systemd服务单元文件,具有.service后缀,这些文件通常位于以下目录
# 系统或用户自定义的配置文件
/etc/systemd/system/
# 软件运行时生成的配置文件
/run/systemd/system
# 系统或第三方软件安装时添加的配置文件。
/usr/lib/systemd/system
Systemd 默认从目录 /etc/systemd/system/ 读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/,真正的配置文件存放在那个目录。
1、service组成部分
service文件主要三大模块
[Unit]:定义与Unit类型无关的通用选项,用于提供unit的描述信息、unit行为及依赖关系等[Service]:定义如何启动、停止、重启当前服务。[Install]:定义如何安装这个配置文件,即怎样做到开机启动。
2 Unit部分
Description: # 对服务的简单描述
After: # 在哪些服务之后启动
Before: # 在哪些服务器启动之前启动
Requires: # 可以指定服务依赖于哪些服务(强依赖)
Wants: # 可以指定服务依赖于哪些服务(弱依赖)
3、Service部分
EnvironmentFile: 指定当前服务启动的环境变量
ExecStart: 指定服务启动时执行的命令或脚本
ExecStop: 指明停止服务要运行的命令或脚本
RestartSec: 指定服务在重启时等待的时间,单位为秒
ExecReload: 指明重启服务要运行的命令或脚本
Restart: 重启设置
KillMode: 指定停止的方式
Restart: 指定重启时的类型
Type: 指定启动类型,
type的可选值
simple 指定ExecStart字段的进程为主进程
forking 指定以fork() 子进程执行ExecStart字段的进程
oneshot 执行一次
notify 启动后发送会发送通知信号通知systemd
idle 等其他任务结束后才运行
Restart的可选值
no: 退出后不会重启
on-success: 当进程正常退出时(退出码为0) 执行重启
on-failure: 当进程不正常退出时(退出码不为0) 执行重启
on-abnormal: 当被信号终止和超时执行重启on-abort: 当收到没有捕捉到的信号终止时执行重启
on-watchdog: 当看门狗超时时执行重启
always: 一直重启
KillMode可选值
control-group: 杀掉当前进程中所有的进程
process: 杀掉当前进程的主进程
mixed: 主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
none: 不杀掉任何进程
4、Install部分
Alias: 别名,可使用systemctl command Alias.service
RequiredBy: 被哪些units所依赖,强依赖
WantedBy: 被哪些units所依赖,弱依赖
Also: 安装本服务的时候还要安装别的相关服务
Install: 一般填为WantedBy=multi-user.target
三、service文件样例
将自己部署的nginx注册为系统服务
[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 /data/nginx/conf/nginx.conf
#重载配置文件命令
ExecReload=/data/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/data/nginx/sbin/nginx -s quit[Install]
WantedBy=multi-user.target
将部署的mysql注册为系统服务
[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命令合集
#启动服务
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