如何知道模板 systemctl 已停止

如何知道模板 systemctl 已停止

我认为我的问题与此类似:

https://askubuntu.com/questions/941775/stopped-custom-services-control-by-systemd-do-not-appear-in-systemctl-list-un

但它没有得到任何有用的答案,所以我再试一次!

我需要的是能够知道停止的模板服务是否曾经启动过。

这是我的服务:

$ cat /etc/systemd/system/[email protected] 
[Unit]
Description=Systemctl Example
StartLimitIntervalSec=0
StartLimitBurst=0

[Service]
ExecStart=/bin/echo %i >> /opt/template.log
Restart=always
RestartSec=1s

[Install]
WantedBy=multi-user.target

如果我启动然后停止该服务的一个实例,它不会出现在列表单位列表:

$ sudo systemctl start example@100
$ sudo systemctl start example@200
$ sudo systemctl stop example@200
$ systemctl list-units --full --all --plain --no-legend --no-pager|grep example
[email protected]                                                                                                loaded    activating auto-restart Systemctl Example
system-example.slice                                                                                               loaded    active     active       Slice /system/example
$ service example@100 status[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/example@ [...]
     Active: activating (auto-restart) since Wed 2022-11-02 11:20:54 CET; 799ms ago
    Process: 41042 ExecStart=/bin/echo 100 >> /opt/template.log (code=exited, status=0/SUCCESS)
   Main PID: 4102 (code=exited, status=0/SUCCESS)
        CPU: 1ms
nov. 02 11:20:54 systemd[1]: Started Systemctl Example.
nov. 02 11:20:55 echo[41042]: 100 >> /opt/template.log
nov. 02 11:20:54 systemd[1]: [email protected]: Deactivated successfully.

我期望与“非模板”服务具有相同的行为:

$ sudo systemctl stop bluetooth
$ systemctl list-units --full --all --plain --no-legend --no-pager|grep bluetooth
bluetooth.service                                                                                               loaded    inactive   dead         Bluetooth service
bluetooth.target                                                                                                loaded    active     active       Bluetooth Support

地位如果服务是模板或不是,选项会有很大不同:

$ systemctl status bluetooth
○ bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2022-10-31 11:47:25 CET; 2min 18s ago
       Docs: man:bluetoothd(8)
    Process: 1071 ExecStart=/usr/lib/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
   Main PID: 1071 (code=exited, status=0/SUCCESS)
     Status: "Powering down"
        CPU: 56ms

$ systemctl status example@200 # this service instance has just been stopped[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
     Active: inactive (dead)

$ systemctl status toto@2000 # this service instance has never been started[email protected] - Systemctl Example
     Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
     Active: inactive (dead)

为什么模板服务中缺少“since xxxxxx; Ymin ago”?

我使用的是 Ubuntu 22.04,但 Ubuntu 18.04 上也有同样的问题。

$ systemctl --version
systemd 249 (249.11-0ubuntu3.6)

答案1

这可能对你有帮助:

sudo journalctl -xe -u example@200

这将显示有关该单元的日志(诸如开始、完成、停止等事件由该命令处理)。

例如,就我而言,我停止了设备[email protected],当我运行时,我得到了以下输出:sudo journalctl -xe -u [email protected]

Oct 31 20:53:11 localhost.localdomain systemd[1]: Stopping Getty on tty1...
░░ Subject: A stop job for unit [email protected] has begun execution
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A stop job for unit [email protected] has begun execution.
░░ 
░░ The job identifier is 13406.
Oct 31 20:53:11 localhost.localdomain systemd[1]: [email protected]: Deactivated successfully.
░░ Subject: Unit succeeded
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit [email protected] has successfully entered the 'dead' state.
Oct 31 20:53:11 localhost.localdomain systemd[1]: Stopped Getty on tty1.
░░ Subject: A stop job for unit [email protected] has finished
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A stop job for unit [email protected] has finished.
░░ 
░░ The job identifier is 13406 and the job result is done

因此您可以grep根据上面的输出来确定设备是否已停止。但您可能需要根据日期和时间进行 grep。

在某些情况下,该单元可能已自行成功完成。因此,不要显示诸如 之类的消息StoppedStopping否则Deactivated输出将类似于:Finished X unit... Subject: A start job for unit UNIT has finished successfully

秘诀1
如果您想从特定日期/时间获取journalctl输出或到特定日期/时间,您可以使用参数--sinceor --until,如果您想grep输出,您可以使用该--grep选项(这样您就可以避免使用像这样的管道journalctl ... | grep something)。根据man journalctl

-S, --since=, -U, --until=

分别开始显示指定日期或晚于指定日期的条目,或者开始显示指定日期或早于指定日期的条目。日期规范应采用“2012-10-30 18:17:16”格式。如果省略时间部分,则假定为“00:00:00”。如果仅省略秒部分,则假定为“:00”。 ...

-g, --grep=

将输出过滤为 MESSAGE= 字段与指定正则表达式匹配的条目。使用与 PERL 兼容的正则表达式

因此,您可以使用类似的方法来获取特定的条目:

sudo journalctl -xe -u [email protected] --since='2022-11-02 12:14'  --grep='Stopped'

提示2:
如果您正在使用用户单位(位于〜/.config/systemd/user/)那么你应该使用:journalctl -xe --user -u example@200。如果您使用 sudo用户单位您可能无法从该命令中看到任何日志(我不确定是否可以将其配置为也能够使用 sudo 显示日志)

相关内容