简体   繁体   中英

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.

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.

Correct is: ("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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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