首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >监视系统服务激活时间

监视系统服务激活时间
EN

Unix & Linux用户
提问于 2015-10-06 23:01:11
回答 1查看 3.7K关注 0票数 2

我希望能够从系统中检索oneshot服务上一次激活所花费的时间。我考虑了以下几种选择,但它们并没有完全说服我:

  1. 计算InactiveEnterTimestamp - InactiveExitTimestamp,例如通过Python中的D总线接口读取它们。这具有服务运行时不一致(=负)的缺点。
  2. 使用ExecStartPreExecStartPost中的助手脚本来存储时间戳,并计算服务退出后的运行时间。
  3. 使用服务可执行文件周围的包装脚本,一旦主可执行文件退出,该脚本将在文件系统的某个位置存储运行时间。
  4. ExecStartPost中使用一个助手脚本来存储#1中计算出来的值。

如果可能的话,我的偏好是#4,如果不是的话,则是#3。你有什么建议?有更好的方法吗?

背景:我正在运行微型RSS,它有一个提要更新程序脚本,我使用systemd计时器定期运行该脚本。我还以同样的方式运行Isync来备份Gmail收件箱的内容。我的最终目标是能够监视每个服务激活所需的时间,并在需要太长时间或很长时间没有运行时发出警报。

编辑:我的服务文件如下所示:

代码语言:javascript
复制
[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

这是计时器:

代码语言:javascript
复制
[Unit]
Description=Tiny Tiny RSS feeds update timer

[Timer]
OnBootSec=1s
OnUnitInactiveSec=120s
Persistent=true
Unit=tt-rss.service

[Install]
WantedBy=timers.target
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2015-10-07 02:45:22

计算InactiveEnterTimestamp - InactiveExitTimestamp

激活时间(在secs中)是以下结果的结果:

代码语言:javascript
复制
(ActiveEnterTimestampMonotonic - InactiveExitTimestampMonotonic) / 1e6

有关详细信息,请参阅文件analyze_plot中的函数analyze.c

但是你应该让RemainAfterExit=yes在你的部门得到ActiveEnterTimestampMonotonic

您可以在没有ExecMainExitTimestampMonotonic - ExecMainStartTimestampMonotonic的情况下计算PostStartExec中的RemainAfterExit

例如,通过Python中的D总线接口读取它们。

您可以使用systemctl提取这些值:

代码语言:javascript
复制
$ systemctl show -p InactiveExitTimestampMonotonic -p ActiveEnterTimestampMonotonic unit
InactiveExitTimestampMonotonic=44364325621
ActiveEnterTimestampMonotonic=44369331083

根据界面稳定性承诺的说法:

代码语言:javascript
复制
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.

我的最终目标是能够监视每个服务激活所需的时间,如果需要太长时间,则会收到警报。

您可以设置TimeoutStartSecOnFailure

代码语言:javascript
复制
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.

或者已经很久没跑了

您可以从日记中提取最后成功的时间:

代码语言:javascript
复制
 journalctl -u your-service MESSAGE='Started your-service.service.'

但是,您应该对日志消息的持久存储进行启用

票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/234388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档