Skip to content Skip to sidebar Skip to footer

Starting A Systemd Service Via Python

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

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

Post a Comment for "Starting A Systemd Service Via Python"