参考链接:阮一峰的博客
概念
Systemd 是 Linux 系统工具,用来启动守护进程(daemon),systemctl是Systemd的主命令
Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位),其中service unit为系统服务
[root@han ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Mon 2022-03-14 14:13:01 CST; 1 weeks 2 days ago
Main PID: 23195 (redis-server)
CGroup: /system.slice/redis.service
└─23195 /usr/bin/redis-server 0.0.0.0:6379
Mar 14 14:13:01 han systemd[1]: Starting Redis persistent key-value database...
Mar 14 14:13:01 han systemd[1]: Started Redis persistent key-value database.
输出结果含义如下。
- Loaded行:配置文件的位置,是否设为开机启动
- Active行:表示正在运行
- Main PID行:主进程ID
- Status行:由应用本身(这里是 redis )提供的软件当前状态
- CGroup块:应用的所有子进程
- 日志块:应用的日志
常用命令
unit管理类
# 列出正在运行的 Unit
systemctl list-units
# 列出所有Unit,包括没有找到配置文件的或者启动失败的
systemctl list-units --all
单个服务相关操作
# 查看服务状态
systemctl status apache.service
#立即启动一个服务
systemctl start apache.service
# 立即停止一个服务
systemctl stop apache.service
# 重启一个服务
systemctl restart apache.service
# 杀死一个服务的所有子进程
systemctl kill apache.service
# 重新加载一个服务的配置文件
systemctl reload apache.service
#设置为开机启动
systemctl enable apache.service
# 查看配置文件
systemctl cat apache.service
服务相关
# 重载所有修改过的配置文件
systemctl daemon-reload
#Systemd 统一管理所有 Unit 的启动日志。带来的好处就是,可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)
journalctl -xe
配置文件
存放目录:
/usr/lib/systemd/system/
样例:
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
Type=simple
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
重要字段说明
配置文件里面最重要的字段是ExecStart。
- ExecStart字段:定义启动进程时执行的命令。 上面的例子中,启动sshd,执行的命令是/usr/sbin/sshd -D $OPTIONS,其中的变量$OPTIONS就来自EnvironmentFile字段指定的环境参数文件。
- ExecReload字段:重启服务时执行的命令
- ExecStop字段:停止服务时执行的命令
- ExecStartPre字段:启动服务之前执行的命令
- ExecStartPost字段:启动服务之后执行的命令
- ExecStopPost字段:停止服务之后执行的命令
所有的启动设置之前,都可以加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如,EnvironmentFile=-/etc/sysconfig/sshd(注意等号后面的那个连词号),就表示即使/etc/sysconfig/sshd文件不存在,也不会抛出错误
- Type:定义启动类型。
- Restart:对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal。
- RestartSec字段:表示 Systemd 重启服务之前,需要等待的秒数。上面的例子设为等待42秒。
实战
将prometheus设置为服务
原运行命令:
nohup prometheus --config.file "/etc/prometheus/prometheus.yml" > /dev/null &
先创建配置文件:
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
[Service]
ExecStart=/usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml
重载配置文件 systemctl daemon-reload,启动
[root@han ~]# systemctl start prometheus
[root@han ~]# systemctl status prometheus
● prometheus.service - prometheus
Loaded: loaded (/usr/lib/systemd/system/prometheus.service; static; vendor preset: disabled)
Active: active (running) since Wed 2022-03-23 14:25:05 CST; 1s ago