使用 systemd 来管理启动项 启动序列( 三 )

从状态检查命令的输出中我们可以看到,systemd 日志表明 hello.sh启动然后服务结束了 。你也可以看到脚本的输出 。该输出是根据服务的最近调用的日志记录生成的,试试看多启动几次这个服务,然后再看状态命令的输出就能理解我所说的 。
你也应该直接查看日志内容,有很多种方法可以实现 。一种办法是指定记录类型标识符,在这个例子中就是 shell 脚本的名字 。它会展示前几次重启和当前会话的日志记录 。如你所见,我已经为这篇文章做了挺长一段时间的研究测试了 。
[root@testvm1 ~]# journalctl -t hello.sh-- Reboot --May 08 15:55:47 testvm1.both.org hello.sh[840]: ###############################May 08 15:55:47 testvm1.both.org hello.sh[840]: ######### Hello World! ########May 08 15:55:47 testvm1.both.org hello.sh[840]: ###############################-- Reboot --May 08 16:01:51 testvm1.both.org hello.sh[840]: ###############################May 08 16:01:51 testvm1.both.org hello.sh[840]: ######### Hello World! ########May 08 16:01:51 testvm1.both.org hello.sh[840]: ###############################-- Reboot --May 10 10:37:49 testvm1.both.org hello.sh[842]: ###############################May 10 10:37:49 testvm1.both.org hello.sh[842]: ######### Hello World! ########May 10 10:37:49 testvm1.both.org hello.sh[842]: ###############################May 10 10:54:45 testvm1.both.org hello.sh[1380]: ###############################May 10 10:54:45 testvm1.both.org hello.sh[1380]: ######### Hello World! ########May 10 10:54:45 testvm1.both.org hello.sh[1380]: ###############################[root@testvm1 ~]#为了定位 hello.service单元的 systemd 记录,你可以在 systemd 中搜索 。你可以使用G+Enter来翻页到日志记录 记录的末尾,然后用回滚来找到你感兴趣的日志 。使用-b选项仅展示最近启动的记录 。
[root@testvm1 ~]# journalctl -b -t systemdMay 10 10:37:49 testvm1.both.org systemd[1]: Starting SYSV: Late init script for live image....May 10 10:37:49 testvm1.both.org systemd[1]: Started SYSV: Late init script for live image..May 10 10:37:49 testvm1.both.org systemd[1]: hello.service: Succeeded.May 10 10:37:49 testvm1.both.org systemd[1]: Finished My hello shell script.May 10 10:37:50 testvm1.both.org systemd[1]: Starting D-Bus System Message Bus...May 10 10:37:50 testvm1.both.org systemd[1]: Started D-Bus System Message Bus.我拷贝了一些其他的日志记录,让你对你可能找到的东西有所了解 。这条命令喷出了所有属于 systemd 的日志内容 —— 当我写这篇时是 109183 行 。这是一个需要整理的大量数据 。你可以使用页面的搜索功能,通常是 less或者你可以使用内置的grep特性 。-g( 或--grep=)选项可以使用兼容 Perl 的正则表达式 。
[root@testvm1 ~]# journalctl -b -t systemd -g hello[root@testvm1 ~]# journalctl -b -t systemd -g hello-- Logs begin at Tue 2020-05-05 18:11:49 EDT, end at Sun 2020-05-10 11:01:01 EDT. --May 10 10:37:49 testvm1.both.org systemd[1]: Starting My hello shell script...May 10 10:37:49 testvm1.both.org systemd[1]: hello.service: Succeeded.May 10 10:37:49 testvm1.both.org systemd[1]: Finished My hello shell script.May 10 10:54:45 testvm1.both.org systemd[1]: Starting My hello shell script...May 10 10:54:45 testvm1.both.org systemd[1]: hello.service: Succeeded.May 10 10:54:45 testvm1.both.org systemd[1]: Finished My hello shell script.[root@testvm1 ~]#

推荐阅读