简体   繁体   English

通过python启用systemd服务

[英]Enable systemd service via python

According to Starting a systemd service via python I can use D-Bus API for starting/stopping Systemd services as following:根据通过 python 启动 systemd 服务,我可以使用D-Bus API来启动/停止 Systemd 服务,如下所示:

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

But what if I want to enable a systemd service there.但是如果我想在那里启用 systemd 服务怎么办。 I already tried replacing RestartUnit with EnableUnit , but I got:我已经尝试用EnableUnit替换RestartUnit ,但我得到了:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method EnableUnit or interface org.freedesktop.systemd1.Manager.

OK.好的。 I figured it out thanks to Terry Spotts.感谢 Terry Spotts 我想通了。

job = manager.EnableUnitFiles(['ssh.service'], False, True)
manager.Reload()

First argument is a list of systemd unit file names.第一个参数是 systemd 单元文件名列表。

Second argument is a boolean controls whether the unit shall be enabled for runtime only (true, /run), or persistently (false, /etc).第二个参数是一个布尔值,控制单元是否应仅在运行时启用(true,/run),或永久启用(false,/etc)。

Third argument is a boolean controls whether symlinks pointing to other units shall be replaced if necessary.第三个参数是一个布尔值,控制是否在必要时替换指向其他单元的符号链接。

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

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