barebones command to see systemd services status

first commit

Mario Nachbaur 7194ddd3

+100
+14
LICENSE
··· 1 + BSD Zero Clause License 2 + 3 + Copyright (c) 2025 Mario Nachbaur 4 + 5 + Permission to use, copy, modify, and/or distribute this software for any 6 + purpose with or without fee is hereby granted. 7 + 8 + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 9 + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10 + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 11 + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12 + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 13 + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14 + PERFORMANCE OF THIS SOFTWARE.
+30
README.md
··· 1 + # status 2 + 3 + A very barebones command to see systemd services status. 4 + 5 + ```sh 6 + $ status 7 + ssh active enabled 8 + caddy active enabled 9 + service inactive enabled 10 + ``` 11 + 12 + ## config 13 + 14 + Create a file in `$XDG_CONFIG_HOME/statusrc` or `$HOME/.config/statusrc` with a service name per line, eg: 15 + 16 + ``` 17 + ssh 18 + caddy 19 + your-service-name 20 + ``` 21 + 22 + Lines starting with `#` will be ignored. 23 + 24 + ## usage 25 + 26 + Just run `status`. 27 + 28 + ## install 29 + 30 + Move the `status` script to your `$PATH`.
+56
status
··· 1 + #!/usr/bin/env python3 2 + 3 + from logging import Logger 4 + from os import getenv 5 + from pathlib import Path 6 + from subprocess import PIPE, run 7 + 8 + logger = Logger(__name__) 9 + 10 + 11 + def main(): 12 + services = read_services() 13 + if not services: 14 + return 15 + actives = get("is-active", services) 16 + enabled = get("is-enabled", services) 17 + row_format = f"{{:{1 + max(map(len, services))}}}{{:{1 + max(map(len, actives))}}}{{:{1 + max(map(len, enabled))}}}" 18 + for index, service in enumerate(services): 19 + print(row_format.format(service, actives[index], enabled[index])) 20 + 21 + 22 + def read_services() -> list[str]: 23 + config = find_configuration() 24 + if config is None: 25 + return [] 26 + path = Path(config) 27 + if not path.exists(): 28 + logger.warning(f"No configuration file found at {path}") 29 + return [] 30 + with open(config, "r") as file: 31 + return list( 32 + filter( 33 + lambda x: not x.startswith("#"), 34 + map(lambda s: s.strip(), file.readlines()), 35 + ), 36 + ) 37 + 38 + 39 + def find_configuration() -> str | None: 40 + xdg = getenv("XDG_CONFIG_HOME") 41 + if xdg: 42 + return f"{xdg}/statusrc" 43 + home = getenv("HOME") 44 + if home: 45 + return f"{home}/.config/statusrc" 46 + return None 47 + 48 + 49 + def get(subcommand: str, services: list[str]) -> list[str]: 50 + command = ["systemctl", subcommand] + services 51 + result = run(command, stdout=PIPE) 52 + return result.stdout.decode("utf-8").splitlines() 53 + 54 + 55 + if __name__ == "__main__": 56 + main()