Starting a systemd service via python

孤者浪人 提交于 2019-12-06 18:52:47

问题


Is there a way to start/restart a systemd service via python?

I know that I can make a system call - but then I also could write this in shell script...

from subprocess import call
call(["systemctl", "restart service"])

I heared systemd has some python binds, but as far as I saw it they only cover the journal


回答1:


You can use systemd's DBus API to call the RestartUnit method of the Manager (need of sufficient privileges, else it won't work)

import dbus
sysbus = dbus.SystemBus()
systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')
job = manager.RestartUnit('sshd.service', 'fail')


来源:https://stackoverflow.com/questions/33646374/starting-a-systemd-service-via-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!