Created
September 6, 2020 21:59
-
-
Save gaurav36/08e7bb376374fd36ba387a08be7b4fd5 to your computer and use it in GitHub Desktop.
sshd service restart example using python dbus API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import dbus | |
bus = dbus.SystemBus() | |
systemd = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') | |
manager = dbus.Interface(systemd, 'org.freedesktop.systemd1.Manager') | |
def restart(service): | |
""" | |
restart method will restart service that is passed in this method. | |
It raise exception if there is error | |
:param str service: name of the service | |
""" | |
try: | |
manager.RestartUnit(service, 'replace') | |
except: | |
sys.exit("Error: Failed to restart {}.".format(service)) | |
if __name__ == "__main__": | |
restart("sshd.service") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment