Package: servicehandler
Systemd service handler (servicehandler)servicehandleris a Python library that provides an orchestrator for systemd services. It abstracts services as objects and implements helper methods, wrapping thesystemctlcommandUsing this package doesnotrequire root permissions, as the service manager used is the one of the current user (the service configuration files are in/usr/lib/systemd/user/)DescriptionThe first thing to handle services is to create aservice unit file(in/usr/lib/systemd/user/my-service.service) like the following:[Unit]Description=My service[Service]ExecStart=/usr/bin/python3 /home/user/service_script/main.pyEnvironment=PATH=/bin:/usr/bin:/usr/local/binWorkingDirectory=/home/user/service_script/[Install]WantedBy=multi-user.targetDepending on how you created the service file you may need to provide access to the user throughsudo chmod 644 my-service.serviceUsageControl the state of a serviceimportservicehandlerassh# Create a new service handlermy_service=sh.ServiceHandler('MyService','my-service.service')# Check current statemy_service.state()<ServiceState.STOPPED:2># Start the service>my_service.start()MyServicechangedstatetoServiceState.RUNNING<Response.OK:1># Try to start again the service>my_service.start()<Response.ALREADY:2># Terminate the service>my_service.stop()MyServicechangedstatetoServiceState.STOPPED<Response.OK:1># Kill the service# In this specific case, the unit file was configured with restart=on-failure (automatic restart)>my_service.kill()<Response.OK:1>Control the enablement_state of a service (whether it starts automatically on system startup)# Check current enablement_state>my_service.enablement_state()<ServiceEnablementState.DISABLED:2># Enable the service>my_service.enable()Createdsymlink/home/user/.config/systemd/user/multi-user.target.wants/my_service.service→/usr/lib/systemd/user/my_service.service.MyServicechangedenablementstatetoServiceEnablementState.ENABLED<Response.OK:1># Disable the service>my_service.disable()Removed/home/user/.config/systemd/user/multi-user.target.wants/my_service.service.MyServicechangedenablementstatetoServiceEnablementState.DISABLED<Response.OK:1>Iterate over different services and perform batch operationsimportservicehandlerasshservice_A=sh.ServiceHandler('ServiceA','A-config-file.service')service_B=sh.ServiceHandler('ServiceB','B-config-file.service')service_C=sh.ServiceHandler('ServiceC','C-config-file.service')services=[ServiceA,ServiceB,ServiceC]# Iterate over the services easilyforsrinservices:ifsr.state==sh.ServiceStatus.STOPPED:sr.restart()print(sr)InstallationInstall using pipThis package is available onPyPIand it can be installed usingpip:pip install servicehandlerBuild from sourceTo build and install the package from source:git clone https://github.com/albertosantagostino/systemd-servicehandler
cd systemd-servicehandler
python3 setup.py installDevelopment history and use casesManage multiple services from a single entry-pointThis library was developed while working on a Telegram botoverlordmanager, used to handle other bots (and services) running on the same platform, providing a single point of access to the userIn this scenario multiple bots run on a headless Raspberry Pi Zero. In order to start them when needed, check their logs and interact with them without opening an SSH session every time, a brand new all-powerful Telegram bot was created, weaponized with this new packageLicenseThe package is distributed under theMIT License