简体   繁体   English

通过python启动systemd服务

[英]Starting a systemd service via python

Is there a way to start/restart a systemd service via python? 有没有办法通过python启动/重启systemd服务?

I know that I can make a system call - but then I also could write this in shell script... 我知道我可以进行系统调用 - 但是我也可以用shell脚本编写这个...

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 我听说systemd有一些python绑定,但据我所见,它们只覆盖了期刊

You can use systemd's DBus API to call the RestartUnit method of the Manager (need of sufficient privileges, else it won't work) 您可以使用systemd的DBus API来调用ManagerRestartUnit方法(需要足够的权限,否则它将无法工作)

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')

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

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