[docs]defis_systemd()->bool:"""Check if the system is using systemd. Returns: True if the system uses systemd else False Examples: >>> distrax.utils.system.is_systemd() True """# If this is the case the file /proc/1/comm will have systemd within itPROCESS_PATH="/proc/1/comm"ifos.path.exists(PROCESS_PATH):file=open(PROCESS_PATH)else:returnFalseforlineinfile:ifre.search("systemd",line):returnTruereturnFalse
[docs]defenable_service(service:str)->None:"""Enables systemd service. Args: service: The systemd service to enable Examples: >>> distrax.utils.system.enable_service("service_to_enable") """ifis_systemd():command=["sudo","systemctl","enable","{service}".format(service=service),]subprocess.run(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
[docs]defdisable_service(service:str)->None:"""Disables systemd service. Args: service: The systemd service to disable Examples: >>> distrax.utils.system.disable_service("service_to_disable") """ifis_systemd():command=["sudo","systemctl","disable",f"{service}".format(service=service),]subprocess.run(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
[docs]defis_systemd_service_enabled(service:str)->bool:"""Check if systemd service is enabled or not. Args: service: The systemd process to check Returns: True if enabled else False Examples: >>> distrax.utils.system.is_systemd_service_enabled("enabled_service") True >>> distrax.utils.system.is_systemd_service_enabled("disabled_service") False """result=subprocess.run(["systemctl","is-enabled","--quiet","{service}".format(service=service),])returnresult.returncode==0
[docs]defstart_service(service:str)->None:"""Start systemd service. Args: service: The systemd service to start Examples: >>> distrax.utils.system.start_service("service_to_start") """ifis_systemd():command=["sudo","systemctl","start","{service}".format(service=service),]subprocess.run(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
[docs]defstop_service(service:str)->None:"""Stops systemd service. Args: service: The systemd service to stop running Examples: >>> distrax.utils.system.stop_service("service_to_stop") """ifis_systemd():command=["sudo","systemctl","stop","{service}".format(service=service),]subprocess.run(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
[docs]defis_systemd_service_active(service:str)->bool:"""Check if systemd service is active or not. Args: service: The systemd process to check Returns: True if enabled else False Examples: >>> distrax.utils.system.is_systemd_service_active("active_service") True >>> distrax.utils.system.is_systemd_service_active("stopped_service") False """result=subprocess.run(["systemctl","is-active","--quiet","{service}".format(service=service),])returnresult.returncode==0
[docs]deffree_memory()->int:"""Get the amount of free RAM available on the system. Returns: Amount of memory in KiB Examples: >>> free_memory() 14623096 """withopen("/proc/meminfo")asfile:forlineinfile:if"MemFree"inline:free_mem=line.split()[1]returnint(free_mem)return0