Skip to content

Instantly share code, notes, and snippets.

@gaurav36
Created September 6, 2020 21:59
Show Gist options
  • Save gaurav36/08e7bb376374fd36ba387a08be7b4fd5 to your computer and use it in GitHub Desktop.
Save gaurav36/08e7bb376374fd36ba387a08be7b4fd5 to your computer and use it in GitHub Desktop.
sshd service restart example using python dbus API
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