通过python启动systemd服务 [英] Starting a systemd service via python

查看:39
本文介绍了通过python启动systemd服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过python启动/重启systemd服务?

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

我知道我可以进行系统调用 - 但是我也可以在 shell 脚本中编写它...

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"])

我听说 systemd 有一些 python 绑定,但据我所知,它们只涵盖期刊

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

推荐答案

你可以使用 systemd 的 DBus API调用ManagerRestartUnit方法(需要足够的权限,否则无法运行)

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

这篇关于通过python启动systemd服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆