[英]Howto create systemd transient timer and service via python and dbus (systemd-run like)?
我想啟動一個不存在的計時器,它應該在當時啟動一個不存在的服務。 這應該通過 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
問題是,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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.