systemd服务管理详解(将部署的服务注册为系统服务)

一、概述

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、组成部分详解

2.1 Unit部分

Description:    对当前服务的简单描述
After:      可以指定在哪些服务之后进行启动
Requires:     可以指定服务依赖于哪些服务(强依赖)
Wants:     可以指定服务依赖于哪些服务(弱依赖)
Conflicts:    定义units间的冲突关系

 2.2 Service部分

EnvironmentFile:  环境配置文件,用来指定当前服务启动的环境变量
ExecStart:  指定服务启动时执行的命令或脚本
ExecStartPre:  指定服务启动前执行的命令或脚本
ExecStop:    指明停止服务要运行的命令或脚本
RestartSec:    指定服务在重启时等待的时间,单位为秒
ExecReload:    指明重启服务要运行的命令或脚本
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:        一直重启

2.3 Install部分

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

三、service文件样例

1、将自己部署的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=/usr/local/nginx/sbin/nginx -t
#启动命令
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#重载配置文件命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

2、将部署的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 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

#查看服务是否开机自启
systemctl is-enabled name.service

#列出失败的服务
systemctl --failed --type=service


#查看服务的依赖关系
systemctl list-dependencies name.service

#杀掉进程
systemctl kill unitname

#重新加载配置文件
systemctl daemon-reload

#重启服务器
systemctl reboot

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维李哥不背锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值