duanlu1959 2017-07-06 10:31
浏览 372
已采纳

如何注册systemd服务生命周期事件?

I want to be informed about all state changes of service units. I know there is the DBUS API which gives me signals for UnitNew and UnitRemoved, but the semantic is still unclear to me. Also the signals doesn't provide the informations I need.

I'm using go and tried the go-systemd lib from coreos. They provide a way to "subscribe" but it's in fact polling all the unit information available in the system. I do not want to do so every few milliseconds, just to get informed about statechanges.

I also tried the default org.freedesktop.DBus.Properties but I can not get this working.

Please help If you can, I'm out of ideas.

  • 写回答

1条回答 默认 最新

  • dsvyc66464 2017-07-09 13:39
    关注

    The systemd APIs that contain information about the start or stop of service units are:

    • The systemd journal. You can listen to it by executing journalctl or forwarding it via network to your program or watching for changes to the files on disk. E.g.: execute journalctl --follow -o json-pretty _PID=1 (you can add various filters like UNIT=gdm.service to only get those for one service) and then parse stdout according to the systemd journal json output format to get the journal messages of systemd. Use the MESSAGE_ID to match what the event means and watch for unknown messages to know when an existing message got replaced with a new one. While this might work robustly enough, log messages are not primarily intended as an API. As such I would recommend to use the below options.

    • Linux cgroup notifications as they get generated when systemd uses cgroups to start a service.

    • systemd units via their dependencies (like Wants=)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛