如何通过 python 和 dbus (systemd-run like) 创建 systemd 瞬态计时器和服务?

Posted

技术标签:

【中文标题】如何通过 python 和 dbus (systemd-run like) 创建 systemd 瞬态计时器和服务?【英文标题】:Howto create systemd transient timer and service via python and dbus (systemd-run like)? 【发布时间】:2020-05-14 17:30:06 【问题描述】:

我想启动一个不存在的计时器,它应该在当时启动一个不存在的服务。这应该通过 systemd 瞬态单元(如 systemd-run)来完成。

当我执行以下代码时,我收到一个异常(见下文)。谁能告诉我,异常想告诉我什么?

import dbus
import time

proxy = dbus.SystemBus().get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
systemd = dbus.Interface(proxy, dbus_interface="org.freedesktop.systemd1.Manager")
future = time.time() + 15
job = systemd.StartTransientUnit( \
    "bla-foo.timer", "replace", \
        [ \
            ("Description", "Bla Foo Timer"), \
            ("RemainAfterElapse", False), \
            ("OnCalendar", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(future))) \
        ], \
        [("bla-foo.service", \
            [ \
                ("Description", "Bla Foo Service"), \
                ("ExecStart", ("/usr/bin/python3", ["-c", "import os; print(os.getcwd())"], False)), \
                ("Type", "oneshot"), \
                ("WorkingDirectory", "/usr/lib") \
            ] \
        )] \
    )
print(job)
Traceback (most recent call last):
  File "/usr/lib/swmanager/preinstaller/test.py", line 19, in <module>
    ("WorkingDirectory", "/usr/lib") \
  File "/usr/lib/python3.7/site-packages/dbus/proxies.py", line 72, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python3.7/site-packages/dbus/proxies.py", line 147, in __call__
    **keywords)
  File "/usr/lib/python3.7/site-packages/dbus/connection.py", line 653, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: System.Error.ENXIO: No such device or address

【问题讨论】:

【参考方案1】:

问题是,ExecStart 参数的格式不正确。

正确的是: ("ExecStart", [("/usr/bin/python3", [ "/usr/bin/python3", "-c", "import os; print(os.getcwd())"], True)]),

完整的工作代码:

import dbus
import time

proxy = dbus.SystemBus().get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
systemd = dbus.Interface(proxy, dbus_interface="org.freedesktop.systemd1.Manager")
future = time.time() + 15
job = systemd.StartTransientUnit( \
    "bla-foo.timer", "replace", \
        [ \
            ("Description", "Bla Foo Timer"), \
            ("RemainAfterElapse", False), \
            ("OnCalendar", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(future))) \
        ], \
        [("bla-foo.service", \
            [ \
                ("Description", "Bla Foo Service"), \
                ("ExecStart", [("/usr/bin/python3", ["/usr/bin/python3", "-c", "import os; print(os.getcwd())"], True)]), \
                ("Type", "oneshot"), \
                ("WorkingDirectory", "/usr/lib") \
            ] \
        )] \
    )
print(job)

【讨论】:

以上是关于如何通过 python 和 dbus (systemd-run like) 创建 systemd 瞬态计时器和服务?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 dbus 调用 varargs 函数?

问题在python中打印usb的挂载点

通过 python 和 dbus 启动用户 systemd 服务

通过 Python 的 dbus 模块控制 CentOS 中的 firewalld?

Python异步和dbus

Bluez BLE 连接监控使用 DBUS-Python