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


如果您只想查看包含hello服务的日志记录,可以指定一个时间来缩小范围 。例如,我将在我的测试虚拟机上的10:54:00开始,这是上述日志记录开始的分钟数 。注意-自=的选项必须加引号,这个选项也可以写成-S“在某个时间” 。
您的计算机上的日期和时间可能不同,因此请确保使用与日志中的时间相匹配的时间戳 。
[root@testvm1~]#journalctl--since="2020-05-1010:54:00"May1010:54:35testvm1.both.orgaudit:BPFprog-id=54op=LOADMay1010:54:35testvm1.both.orgaudit:BPFprog-id=55op=LOADMay1010:54:45testvm1.both.orgsystemd[1]:StartingMyhelloshellscript...May1010:54:45testvm1.both.orghello.sh[1380]:###############################May1010:54:45testvm1.both.orghello.sh[1380]:#########HelloWorld!########May1010:54:45testvm1.both.orghello.sh[1380]:###############################May1010:54:45testvm1.both.orgsystemd[1]:hello.service:Succeeded.May1010:54:45testvm1.both.orgsystemd[1]:FinishedMyhelloshellscript.May1010:54:45testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd"'May1010:54:45testvm1.both.orgaudit[1]:SERVICE_STOPpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd/"'May1010:56:00testvm1.both.orgNetworkManager[840]:由于选项会跳过指定时间点的所有记录,但在此时间点之后仍有许多记录不需要 。您也可以使用直到选项在您感兴趣的时间之后剪切记录 。我只想在事发时间附近呆一分钟,别无他求:
[root@testvm1~]#journalctl--since="2020-05-1010:54:35"--until="2020-05-1010:55:00"--LogsbeginatTue2020-05-0518:11:49EDT,endatSun2020-05-1011:04:59EDT.--May1010:54:35testvm1.both.orgsystemd[1]:Reloading.May1010:54:35testvm1.both.orgaudit:BPFprog-id=27op=UNLOADMay1010:54:35testvm1.both.orgaudit:BPFprog-id=26op=UNLOADay1010:54:35testvm1.both.orgaudit:BPFprog-id=55op=LOADMay1010:54:45testvm1.both.orgsystemd[1]:StartingMyhelloshellscript...May1010:54:45testvm1.both.orghello.sh[1380]:###############################May1010:54:45testvm1.both.orghello.sh[1380]:#########HelloWorld!########May1010:54:45testvm1.both.orghello.sh[1380]:###############################May1010:54:45testvm1.both.orgsystemd[1]:hello.service:Succeeded.May1010:54:45testvm1.both.orgsystemd[1]:FinishedMyhelloshellscript.May1010:54:45testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd>May1010:54:45testvm1.both.orgaudit[1]:SERVICE_STOPpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd/>lines1-46/46(END)如果此时间段内仍有大量活动,您可以使用以下选项进一步缩小结果数据流:
[root@testvm1~]#journalctl--since="2020-05-1010:54:35"--until="2020-05-1010:55:00"-t"hello.sh"--LogsbeginatTue2020-05-0518:11:49EDT,endatSun2020-05-1011:10:41EDT.--May1010:54:45testvm1.both.orghello.sh[1380]:###############################May1010:54:45testvm1.both.orghello.sh[1380]:#########HelloWorld!########May1010:54:45testvm1.both.orghello.sh[1380]:###############################[root@testvm1~]#你的结果应该和我的差不多 。从这一系列实验中可以看出,该服务运行正常 。
重启-或者到此为止 。到目前为止,您还没有重新启动安装了该服务的机器 。所以现在重启,因为毕竟这个教程是关于程序在启动阶段的运行 。首先,您需要在启动序列中启用该服务 。
[root@testvm1~]#systemctlenablehello.serviceCreatedsymlink/etc/systemd/system/multi-user.target.wants/hello.service→/etc/systemd/system/hello.service.[root@testvm1~]#请注意,此软链接是在/etc/systemd/system/multi-user . target . wants目录中创建的 。这是因为服务单元文件指定多用户“需要”该服务 。
重启机器,确保在启动阶段可以观察到数据流,这样就可以看到“Hello world”的消息 。等待...你看到了吗?是的,我看到了 。虽然很快就被刷掉了,但我确实看到systemd的信息显示它启动了hello.service服务 。
查看上次系统启动后的日志 。你可以少用页面搜索工具找“你好”或者“你好” 。我切掉了很多数据,但是留下了附近的日志记录,这样您就可以在本地感受到与您的服务相关的日志记录是什么样子:
[root@testvm1~]#journalctl-bMay1010:37:49testvm1.both.orgsystemd[1]:ListeningonSSSDKerberosCacheManagerrespondersocket.May1010:37:49testvm1.both.orgsystemd[1]:ReachedtargetSockets.May1010:37:49testvm1.both.orgsystemd[1]:ReachedtargetBasicSystem.May1010:37:49testvm1.both.orgsystemd[1]:StartingModemManager...May1010:37:49testvm1.both.orgsystemd[1]:StartingNetworkManager...May1010:37:49testvm1.both.orgsystemd[1]:StartingAvahimDNS/DNS-SDStack...May1010:37:49testvm1.both.orgsystemd[1]:ConditioncheckresultedinSecureBootDBX(blacklist)updaterbeingskipped.May1010:37:49testvm1.both.orgsystemd[1]:StartingMyhelloshellscript...May1010:37:49testvm1.both.orgsystemd[1]:StartingIPv4firewallwithiptables...May1010:37:49testvm1.both.orgsystemd[1]:Startedirqbalancedaemon.May1010:37:49testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=irqbalancecomm="systemd"exe="/usr/lib/sy>"'May1010:37:49testvm1.both.orgsystemd[1]:StartingLSB:Initscriptforliveimage....May1010:37:49testvm1.both.orgsystemd[1]:StartingHardwareMonitoringSensors...May1010:37:49testvm1.both.orgsystemd[1]:StartingNTPclient/server...May1010:37:49testvm1.both.orgsystemd[1]:StartingSYSV:Lateinitscriptforliveimage....May1010:37:49testvm1.both.orgsystemd[1]:StartedSYSV:Lateinitscriptforlive资源网image..May1010:37:49testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=livesys-latecomm="systemd"exe="/usr/lib/>"'May1010:37:49testvm1.both.orghello.sh[842]:###############################May1010:37:49testvm1.both.orghello.sh[842]:#########HelloWorld!########May1010:37:49testvm1.both.orghello.sh[842]:###############################May1010:37:49testvm1.both.orgsystemd[1]:hello.service:Succeeded.May1010:37:49testvm1.both.orgsystemd[1]:FinishedMyhelloshellscript.May1010:37:49testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd>"'May1010:37:49testvm1.both.orgaudit[1]:资源网SERVICE_STOPpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd/>May1010:37:50testvm1.both.orgaudit:BPFprog-id=28op=LOAD


推荐阅读