我希望能够从系统中检索oneshot服务上一次激活所花费的时间。我考虑了以下几种选择,但它们并没有完全说服我:
InactiveEnterTimestamp - InactiveExitTimestamp,例如通过Python中的D总线接口读取它们。这具有服务运行时不一致(=负)的缺点。ExecStartPre和ExecStartPost中的助手脚本来存储时间戳,并计算服务退出后的运行时间。ExecStartPost中使用一个助手脚本来存储#1中计算出来的值。如果可能的话,我的偏好是#4,如果不是的话,则是#3。你有什么建议?有更好的方法吗?
背景:我正在运行微型RSS,它有一个提要更新程序脚本,我使用systemd计时器定期运行该脚本。我还以同样的方式运行Isync来备份Gmail收件箱的内容。我的最终目标是能够监视每个服务激活所需的时间,并在需要太长时间或很长时间没有运行时发出警报。
编辑:我的服务文件如下所示:
[Unit]
Description=Tiny Tiny RSS feeds update
After=network.target mysqld.service postgresql.service
[Service]
Type=oneshot
ExecStart=/usr/bin/php /usr/share/webapps/tt-rss/update.php --feeds
User=ttrss
StandardOutput=syslog
StandardError=syslog这是计时器:
[Unit]
Description=Tiny Tiny RSS feeds update timer
[Timer]
OnBootSec=1s
OnUnitInactiveSec=120s
Persistent=true
Unit=tt-rss.service
[Install]
WantedBy=timers.target发布于 2015-10-07 02:45:22
计算InactiveEnterTimestamp - InactiveExitTimestamp
激活时间(在secs中)是以下结果的结果:
(ActiveEnterTimestampMonotonic - InactiveExitTimestampMonotonic) / 1e6有关详细信息,请参阅文件analyze_plot中的函数analyze.c。
但是你应该让RemainAfterExit=yes在你的部门得到ActiveEnterTimestampMonotonic。
您可以在没有ExecMainExitTimestampMonotonic - ExecMainStartTimestampMonotonic的情况下计算PostStartExec中的RemainAfterExit。
例如,通过Python中的D总线接口读取它们。
您可以使用systemctl提取这些值:
$ systemctl show -p InactiveExitTimestampMonotonic -p ActiveEnterTimestampMonotonic unit
InactiveExitTimestampMonotonic=44364325621
ActiveEnterTimestampMonotonic=44369331083根据界面稳定性承诺的说法:
The stable interfaces are:
...
The command line interface of systemctl, loginctl, journalctl.
We will make sure that scripts invoking these commands will continue
to work with future versions of systemd. Note however that the output
generated by these commands is generally not included in the promise,
unless it is documented in the man page. Example: the output of
"systemctl status" is not stable, but the one of "systemctl show" is,
because the former is intended to be human readable and the latter
computer readable, and this is documented in the man page.我的最终目标是能够监视每个服务激活所需的时间,如果需要太长时间,则会收到警报。
您可以设置TimeoutStartSec和OnFailure:
TimeoutStartSec=
Configures the time to wait for start-up. If a daemon service does
not signal start-up completion within the configured time, the
service will be considered failed and will be shut down again.
OnFailure=
A space-separated list of one or more units that are activated when
this unit enters the "failed" state.或者已经很久没跑了
您可以从日记中提取最后成功的时间:
journalctl -u your-service MESSAGE='Started your-service.service.'但是,您应该对日志消息的持久存储进行启用。
https://unix.stackexchange.com/questions/234388
复制相似问题