Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anabarasan/36184c51c0972efa6e4a8d01ddc94e9f to your computer and use it in GitHub Desktop.
Save anabarasan/36184c51c0972efa6e4a8d01ddc94e9f 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