Skip to content

Instantly share code, notes, and snippets.

@MorningLightMountain713
Forked from gaurav36/dbus_api.py
Created October 7, 2024 13:44
Show Gist options
  • Save MorningLightMountain713/3f5b787e82d38d24bca92efe1ad7cad4 to your computer and use it in GitHub Desktop.
Save MorningLightMountain713/3f5b787e82d38d24bca92efe1ad7cad4 to your computer and use it in GitHub Desktop.
Using dbus API in python for managing services in Linux
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 disable_and_stop(service):
"""
disable_and_stop and stop method will check if the service is already running or not in system.
If its already running then it will first stop the service. After that it will check if service
is already enabled or not. If its enabled then it will disabled the service.
It raise exception if there is error
:param str service: name of the service
"""
if is_service_active(service):
try:
manager.StopUnit(service, 'replace')
except:
sys.exit("Error: Failed to stop {}.".format(service))
if is_service_enabled(service):
try:
manager.DisableUnitFiles([service], False)
except:
sys.exit("Error: Failed to disable {}.".format(service))
def enable(service):
"""
enable method will enable service that is passed in this method.
It raise exception if there is error
:param str service: name of the service
"""
try:
manager.EnableUnitFiles([service], False, True)
except:
sys.exit("Error: Failed to enable {}.".format(service))
def start(service):
"""
start method will start service that is passed in this method.
If service is already started then it will ignore it.
It raise exception if there is error
:param str service: name of the service
"""
try:
manager.StartUnit(service, 'replace')
except:
sys.exit("Error: Failed to start {}.".format(service))
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))
def is_service_enabled(service):
"""
is_service_enabled method will check if service is already enabled that
is passed in this method.
It raise exception if there is error.
Return value, True if service is already enabled otherwise False.
:param str service: name of the service
"""
try:
return manager.GetUnitFileState(service) == 'enabled'
except:
return False
def is_service_active(service):
"""
is_service_active method will check if service is running or not.
It raise exception if there is service is not loaded
Return value, True if service is running otherwise False.
:param str service: name of the service
"""
try:
manager.GetUnit(service)
return True
except:
return False
def enable_and_start(service):
"""
enable_and_start method will enable and start service.
Return value, True if service is running otherwise False.
:param str service: name of the service
"""
try:
if not is_service_enabled(service):
enable(service)
if not is_service_active(service):
start(service)
return True
except:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment