journalctl来查看systemd日志
当我们使用systemd来运行service时,脚本中echo
输出的内容不会显示在Terminal中,我们需要使用systemctl status service-name
来查看service的日志。
但systemctl status
输出的日志信息并不够详细,而且当service
的脚本中运行了两个Process,日志比较多时,可能只够显示最后一个Process的日志。
我们可以使用journalctl
来查看完整的使用systemctl启动的service的日志。几个典型的用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| # 查看最近的systemctl服务日志, 多个服务混在一起的 journalctl -f # 不分页显示日志 journalctl --no-pager # 指定某个服务来显示日志 journalctl -u service-name.service # 开机后某个服务的日志 journalctl -u service-name.service -b # 显示从某个时间点开始的日志 journalctl --since "2018-08-30 14:10:10" # 显示到某个时间点截止的日志 journalctl --until "2018-09-02 12:05:50" # 开始和结束和合起来使用 journalctl --since "2018-08-30 14:10:10" --until "2018-09-02 12:05:50"
|
试验
基础试验脚本
systemctl 运行的脚本: /usr/local/bin/hello_world.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #! /bin/sh start() { echo "Executing Start" echo "Testing 01" echo "Testing 02" echo "Testing 03" } stop() { echo "Stopping Hello World Script" } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 2 start ;; *) exit 1 esac
|
创建hello_world的systemd unit文件: /usr/lib/systemd/system/hello_world.service
。
1 2 3 4 5 6 7 8 9 10 11
| [Unit] Description=Hello World Testing Script [Service] Type=oneshot ExecStart=/usr/local/bin/hello_world.sh start ExecStop=/usr/local/bin/hello_world.sh stop RemainAfterExit=yes [Install] WantedBy=multi-user.target
|
重载service: systemctl daemon-reload
试验:查看echo的日志
Terminal中手动运行service,脚本中的echo在默认情况下是不会显示的Terminal中的。
1 2
| # systemctl start hello_world.service #
|
使用systemctl status service_name
来查看service_name
的日志, 可以看到脚本中命令echo "Testing 01"
的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ● hello_world.service - Hello World Testing Script Loaded: loaded (/usr/lib/systemd/system/hello_world.service; disabled; vendor preset: disabled) Active: active (exited) since 四 2022-10-27 00:24:02 CST; 3s ago Process: 8961 ExecStart=/usr/local/bin/hello_world.sh start (code=exited, status=0/SUCCESS) Main PID: 8961 (code=exited, status=0/SUCCESS) 10月 27 00:24:02 jk-test-app-server systemd[1]: Starting Hello World Testing Script... 10月 27 00:24:02 jk-test-app-server hello_world.sh[8961]: Executing Start 10月 27 00:24:02 jk-test-app-server hello_world.sh[8961]: Testing 01 10月 27 00:24:02 jk-test-app-server hello_world.sh[8961]: Testing 02 10月 27 00:24:02 jk-test-app-server hello_world.sh[8961]: Testing 03 10月 27 00:24:02 jk-test-app-server systemd[1]: Started Hello World Testing Script.
|
也可以使用journalctl -f
来查看对应的日志。
试验: 使用journalctl -f查看完整的日志
执行systemctl restart service_name
时,systemctl status service_name
输出的日志可能不完整。
此时,可以使用journalctl -f
或者journalctl -u service_name
来查看完整的日志。
执行restart
命令, 此时在另一个Terminal中执行journalctl -f
来检测日志。
查看systemctl status service_name
的日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ● hello_world.service - Hello World Testing Script Loaded: loaded (/usr/lib/systemd/system/hello_world.service; disabled; vendor preset: disabled) Active: active (exited) since 三 2022-10-26 22:11:28 CST; 1s ago Process: 8556 ExecStop=/usr/local/bin/hello_world.sh stop (code=exited, status=0/SUCCESS) Process: 8557 ExecStart=/usr/local/bin/hello_world.sh start (code=exited, status=0/SUCCESS) Main PID: 8557 (code=exited, status=0/SUCCESS) 10月 26 22:11:28 jk-test-app-server systemd[1]: Starting Hello World Testing Script... 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Executing Start 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 01 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 02 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 03 10月 26 22:11:28 jk-test-app-server systemd[1]: Started Hello World Testing Script.
|
查看journalctl -f
的日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 10月 26 22:11:28 jk-test-app-server polkitd[6072]: Registered Authentication Agent for unix-process:8550:567595 (system bus name :1.71 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) 10月 26 22:11:28 jk-test-app-server systemd[1]: Stopping Hello World Testing Script... 10月 26 22:11:28 jk-test-app-server hello_world.sh[8556]: Stopping Hello World Script 10月 26 22:11:28 jk-test-app-server systemd[1]: Stopped Hello World Testing Script. 10月 26 22:11:28 jk-test-app-server systemd[1]: Starting Hello World Testing Script... 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Executing Start 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 01 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 02 10月 26 22:11:28 jk-test-app-server hello_world.sh[8557]: Testing 03 10月 26 22:11:28 jk-test-app-server systemd[1]: Started Hello World Testing Script. 10月 26 22:11:28 jk-test-app-server polkitd[6072]: Unregistered Authentication Agent for unix-process:8550:567595 (system bus name :1.71, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus) ^C
|
可以看到systemctl status hello_world.service
的输出中,缺少了stop
相关的日志。而journalctl -f
中有完整的日志。
Reference