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

【使用 systemd 来管理启动项 启动序列】您可以看到systemd启动了hello.service单元,该单元执行hello.sh脚本并将输出记录在日志中 。如果您可以在启动阶段捕捉到它,您还应该看到systemd消息指示它正在启动脚本,另一条消息指示服务成功 。通过观察上述数据流中的第一条systemd消息,您会发现systemd在达到基本系统目标后很快就会启动您的服务 。
但我想看到创业阶段印出来的信息 。有一种方法可以做到这一点:在hello.service文件的[Service]部分添加以下行:
StandardOutput=journal+consolehello.service文件如下所示:
#Simpleserviceunitfiletousefortesting#startupconfigurationswithsystemd.#ByDavidBoth#LicensedunderGPLV2#[Unit]Description=Myhelloshellscript[Service]Type=oneshotExecStart=/usr/local/bin/hello.shStandardOutput=journal+console[Install]WantedBy=multi-user.target添加此行后,重启系统,启动时观察显示屏上的数据流滚动 。你应该在它的小盒子里看到信息 。启动序列完成后,您可以查看最新的启动日志,然后找到新服务的日志记录 。
修改顺序现在您的服务可用了,您可以看到它在启动序列中的开始位置,并尝试修改它 。请记住,systemd倾向于在每个主目标(basic.target、多用户. target和graphical)中并行启动尽可能多的服务和其他单元类型 。* *目标) 。你应该刚刚读了上次引导的日志记录 。它应该类似于我上面的日志 。
请注意,systemd在您的测试服务达到基本系统目标后不久就启动了它 。这正是您在服务单元文件的WantedBy行中指定的内容,因此是正确的 。在进行更改之前,请列出目录/etc/systemd/system/multi-user . target . wants中的内容,您将看到一个指向服务单元文件的软链接 。服务单元文件的[安装]部分指定哪个目标将启动服务,执行systemctl enablehlo.service命令将在适当的targets.wants路径下创建一个软链接 。
hello.service->/etc/systemd/system/hello.service有些服务需要在basic.target阶段启动,而有些则不需要,除非系统正在启动graphical.target 。本实验中的服务不会在basic.target阶段启动 。假设您在graphical.target阶段之前不需要它启动 。然后修改所需的行:
WantedBy=graphical.target在重新启用hello.service之前,请务必禁用它,这样您就可以删除旧链接并在目录中创建新链接 。graphic . targets . wants我注意到,如果在修改服务所需的目标之前忘记禁用该服务,我可以运行systemctl disable命令,该链接将从两个目标中删除 。wants目录 。之后,我只需要重新启用该服务并重新启动计算机 。
在图形目标下启动服务时需要注意一点 。如果计算机启动到多用户目标阶段,此服务将不会自动启动 。如果这个服务需要GUI桌面界面,可能是你想要的,但也可能不是你想要的 。
使用-o短单调选项查看graphical.target和multi-user.target的日志,并在内核启动几秒钟后显示日志,精确度为微秒:
[root@testvm1~]#journalctl-b-oshort-monotonic多用户的部分日志 。目标:
[17.264730]testvm1.both.orgsystemd[1]:StartingMyhelloshellscript...[17.265561]testvm1.both.orgsystemd[1]:StartingIPv4firewallwithiptables...[19.478468]testvm1.both.orgsystemd[1]:StartingLSB:Initscriptforliveimage....[19.507359]testvm1.both.orgiptables.init[844]:iptables:Applyingfirewallrules:[OK][19.507835]testvm1.both.orghello.sh[843]:###############################[19.507835]testvm1.both.orghello.sh[843]:#########HelloWorld!########[19.507835]testvm1.both.orghello.sh[843]:###############################[21.482481]testvm1.both.orgsystemd[1]:hello.service:Succeeded.[21.482550]testvm1.both.orgsmartd[856]:Openedconfigurationfile/etc/smartmontools/smartd.conf[21.482605]testvm1.both.orgsystemd[1]:FinishedMyhelloshellscript.还有一些图形目标的日志:
[19.436815]testvm1.both.orgsystemd[1]:StartingMyhelloshellscript...[19.437070]testvm1.both.orgsystemd[1]:StartingIPv4firewallwithiptables...[19.612614]testvm1.both.orghello.sh[841]:###############################[19.612614]testvm1.both.orghello.sh[841]:#########HelloWorld!########[19.612614]testvm1.both.orghello.sh[841]:###############################[19.629455]testvm1.both.orgaudit[1]:SERVICE_STARTpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd/systemd"hostname=?addr=?terminal=?res=success'[19.629569]testvm1.both.orgaudit[1]:SERVICE_STOPpid=1uid=0auid=4294967295ses=4294967295msg='unit=hellocomm="systemd"exe="/usr/lib/systemd/systemd"hostname=?addr=?terminal=?res=success'[19.629682]testvm1.both.orgsystemd[1]:hello.service:Succeeded.[19.629782]testvm1.both.orgsystemd[1]:FinishedMyhelloshellscript.虽然单元文件的WantedBy部分包含graphical.target,但是hello.service单元在启动后大约运行19.5或19.6秒 。但是,hello.service在多用户. target中启动时间为17.24秒,在图形target中为19.43秒 。


推荐阅读