简体   繁体   English

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

[英]Howto create systemd transient timer and service via python and dbus (systemd-run like)?

I want to start a not existing timer which should start a not existing service at the time.我想启动一个不存在的计时器,它应该在当时启动一个不存在的服务。 This should be done via systemd transient unit like systemd-run.这应该通过 systemd 临时单元(如 systemd-run)来完成。

When I execute the following code, I recieve an exception (see below).当我执行以下代码时,我收到一个异常(见下文)。 Can someone please tell me, what the exceptions wants to tell me?有人可以告诉我,例外情况想告诉我什么吗?

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

The problem was, that the ExecStart parameter wasn't in the correct form.问题是,ExecStart 参数的格式不正确。

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

Complete working code:完整的工作代码:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM