Systemd 使用

 常用命令 


使某服务自动启动

systemctl enable httpd.service

使某服务不自动启动

systemctl disable httpd.service

检查服务状态

systemctl status httpd.service (服务详细信息) 
systemctl is-active httpd.service (仅显示是否 Active)

显示所有已启动的服务

systemctl list-units --type=service

启动某服务

systemctl start httpd.service

停止某服务

systemctl stop httpd.service

重启某服务

systemctl restart httpd.service

 

开机服务管理

开机启动 unit

systemctl enable test.service
增加由/lib/systemd/system/到/etc/systemd/system/multi-user.target.wants/下的软链接
ln -s '/usr/lib/systemd/system/postfix.service' '/etc/systemd/system/multi-user.target.wants/test.service'

开机不启动 unit

systemctl disable test.service
删除/etc/systemd/system/multi-user.target.wants下的软链接

查看服务开机是否启动

systemctl is-enabled test.service #查询服务是否开机启动

查看开机自启动程序

ls /etc/systemd/system/multi-user.target.wants/

显示启动失败的单元

systemctl --failed

查看 systemd 管理的所有单元

systemctl list-unit-files

 

日志管理

Systemd 统一管理所有 Unit 的启动日志。带来的好处就是,可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)。日志的配置文件是/etc/systemd/journald.conf。

journalctl功能强大,用法非常多。

# 查看所有日志(默认情况下 ,只保存本次启动的日志)
$ sudo journalctl

# 查看内核日志(不显示应用日志)
$ sudo journalctl -k

# 查看系统本次启动的日志
$ sudo journalctl -b
$ sudo journalctl -b -0

# 查看上一次启动的日志(需更改设置)
$ sudo journalctl -b -1

# 查看指定时间的日志
$ sudo journalctl --since="2012-10-30 18:17:16"
$ sudo journalctl --since "20 min ago"
$ sudo journalctl --since yesterday
$ sudo journalctl --since "2015-01-10" --until "2015-01-11 03:00"
$ sudo journalctl --since 09:00 --until "1 hour ago"

# 显示尾部的最新10行日志
$ sudo journalctl -n

# 显示尾部指定行数的日志
$ sudo journalctl -n 20

# 实时滚动显示最新日志
$ sudo journalctl -f

# 查看指定服务的日志
$ sudo journalctl /usr/lib/systemd/systemd

# 查看指定进程的日志
$ sudo journalctl _PID=1

# 查看某个路径的脚本的日志
$ sudo journalctl /usr/bin/bash

# 查看指定用户的日志
$ sudo journalctl _UID=33 --since today

# 查看某个 Unit 的日志
$ sudo journalctl -u nginx.service
$ sudo journalctl -u nginx.service --since today

# 实时滚动显示某个 Unit 的最新日志
$ sudo journalctl -u nginx.service -f

# 合并显示多个 Unit 的日志
$ journalctl -u nginx.service -u php-fpm.service --since today

# 查看指定优先级(及其以上级别)的日志,共有8级
# 0: emerg
# 1: alert
# 2: crit
# 3: err
# 4: warning
# 5: notice
# 6: info
# 7: debug
$ sudo journalctl -p err -b

# 日志默认分页输出,--no-pager 改为正常的标准输出
$ sudo journalctl --no-pager

# 以 JSON 格式(单行)输出
$ sudo journalctl -b -u nginx.service -o json

# 以 JSON 格式(多行)输出,可读性更好
$ sudo journalctl -b -u nginx.serviceqq
 -o json-pretty

# 显示日志占据的硬盘空间
$ sudo journalctl --disk-usage

# 指定日志文件占据的最大空间
$ sudo journalctl --vacuum-size=1G

# 指定日志文件保存多久
$ sudo journalctl --vacuum-time=1years

 

管理 Systemctl 相关 

在使用过程中如果我们需要建立一个自己的 service 然后用 systemctl 来管理可以参考这个格式以及附录 1 相关的命令

[Unit]
Description=Zendesk maxwell

[Service]
User=maxwell
ExecStart=bash /home/maxwell/start_maxwell.sh
IgnoreSIGPIPE=false
#KillMode=process
SendSIGKILL=no
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

我们建立 unit 的地址通常在

/usr/lib/systemd/system/xxx.service

/lib/systemd/system/xxx.service

另外上面的配置只是配置了 ExecStart 的参数,这只和 systemctl start xxx.service 相关,如果我们要 shutdown 应用,我们上面的配置会在执行 systemctl stop xx.service 的时候默认发送 signal.term 的命令。相关应用接收到之后,可以捕捉这个信号然后进行 grecefull shutdown.

 

 

Reference:

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html  Systemd 入门教程:命令篇

https://blog.csdn.net/qq562029186/article/details/67639676   Linux开机启动管理---systemd使用

https://unix.stackexchange.com/questions/508484/have-systemd-not-kill-your-service-if-it-is-in-a-state-it-should-not-be-killed

posted @ 2019-05-30 18:20  piperck  阅读(1771)  评论(0编辑  收藏  举报