作者:dsalkj08_789 | 来源:互联网 | 2023-09-25 11:19
Linux基础:systemctl和journalctl常用命令「建议收藏」systemctl介绍Systemctl是一个systemd工具,主要负责控制systemd系统和服务管
systemctl 介绍
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
重要:Systemctl接受服务(.service),挂载点(.mount),套接字(.socket)和设备(.device)作为单位。
systemctl 常用 命令
Systemd的版本
systemd --version
systemd和systemctl的二进制文件和库的安装位置
# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
列出所有可用的单位
# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
.....
列出所有运行单元
# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File Syste
sys-devices-pc...0-1:0:0:0-block-sr0.device loaded active plugged VBOX_CD-ROM
sys-devices-pc...:00:03.0-net-enp0s3.device loaded active plugged PRO/1000 MT Desktop Adapter
sys-devices-pc...00:05.0-sound-card0.device loaded active plugged 82801AA AC'97 Audio Controller
sys-devices-pc...:0:0-block-sda-sda1.device loaded active plugged VBOX_HARDDISK
sys-devices-pc...:0:0-block-sda-sda2.device loaded active plugged LVM PV Qzyo3l-qYaL-uRUa-Cjuk-pljo-qKtX-VgBQ8
sys-devices-pc...0-2:0:0:0-block-sda.device loaded active plugged VBOX_HARDDISK
sys-devices-pl...erial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-pl...erial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-pl...erial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-pl...erial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS3
sys-devices-virtual-block-dm\x2d0.device loaded active plugged /sys/devices/virtual/block/dm-0
sys-devices-virtual-block-dm\x2d1.device loaded active plugged /sys/devices/virtual/block/dm-1
sys-module-configfs.device loaded active plugged /sys/module/configfs
...
检查单元是否启用?
[root@10 ~]# systemctl is-enabled kubelet
enabled
检查单元或服务是否正在运行?
[root@10 ~]# systemctl status kubelet -l
● kubelet.service - kubelet: The Kubernetes Node Agent
Loaded: loaded (/etc/systemd/system/kubelet.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/kubelet.service.d
└─10-kubeadm.conf
Active: active (running) since Sat 2019-11-16 02:33:56 CST; 1 weeks 1 days ago
Docs: https://kubernetes.io/docs/
Main PID: 566971 (kubelet)
Memory: 90.4M
CGroup: /system.slice/kubelet.service
└─566971 /usr/bin/kubelet
--bootstrap-kubecOnfig=/etc/kubernetes/bootstrap-kubelet.conf
--kubecOnfig=/etc/kubernetes/kubelet.conf
--cOnfig=/var/lib/kubelet/config.yaml
--cgroup-driver=cgroupfs
--network-plugin=cni
--pod-infra-container-image=10.10.101.175/k8s-deploy/pause:3.1
--system-reserved=memory=782Mi
--system-reserved-cgroup=/system.slice
如何在Linux中启动,重新启动,停止,重新加载和检查服务(httpd.service)的状态
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
如何在引导时激活服务并启用或禁用服务(系统引导时自动启动服务)
# systemctl is-active httpd.service
# systemctl enable httpd.service
# systemctl disable httpd.service
journalctl
Systemd 统一管理所有 Unit 的启动日志。带来的好处就是,可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)。日志的配置文件是/etc/systemd/journald.conf。
journalctl功能强大,用法非常多。
journalctl 常用命令
# 查看所有日志(默认情况下 ,只保存本次启动的日志)
$ journalctl
# 查看内核日志(不显示应用日志)
$ journalctl -k
# 查看系统本次启动的日志
$ journalctl -b
$ journalctl -b -0
# 查看上一次启动的日志(需更改设置)
$ journalctl -b -1
# 查看指定时间的日志
$ journalctl –since=”2012-10-30 18:17:16″
$ journalctl –since “20 min ago”
$ journalctl –since yesterday
$ journalctl –since “2015-01-10” –until “2015-01-11 03:00”
$ journalctl –since 09:00 –until “1 hour ago”
# 显示尾部的最新10行日志
$ journalctl -n
# 显示尾部指定行数的日志
$ journalctl -n 20
# 实时滚动显示最新日志
$ journalctl -f
# 查看指定服务的日志
$ journalctl /usr/lib/systemd/systemd
# 比如查看docker服务的日志
$ systemctl status docker
# 查看指定进程的日志
$ journalctl _PID=1
# 查看某个路径的脚本的日志
$ journalctl /usr/bin/bash
# 查看指定用户的日志
$ journalctl _UID=33 –since today
# 查看某个 Unit 的日志
$ journalctl -u nginx.service
$ journalctl -u nginx.service –since today
# 实时滚动显示某个 Unit 的最新日志
$ 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
$ journalctl -p err -b
# 日志默认分页输出,–no-pager 改为正常的标准输出
$ journalctl –no-pager
# 以 JSON 格式(单行)输出
$ journalctl -b -u nginx.service -o json
# 以 JSON 格式(多行)输出,可读性更好
$ journalctl -b -u nginx.serviceqq -o json-pretty
# 显示日志占据的硬盘空间
$ journalctl –disk-usage
# 指定日志文件占据的最大空间
$ journalctl –vacuum-size=1G
# 指定日志文件保存多久
$ journalctl –vacuum-time=1years