Created
June 17, 2026 03:41
-
-
Save iamtalhaasghar/e0a3bc1ed0352f1ae9f748c30916dc88 to your computer and use it in GitHub Desktop.
Shows you systemd services in a nice list sorted by their last (re-)start time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import subprocess | |
| services = [] | |
| result = subprocess.run( | |
| ["systemctl", "list-units", "--type=service", "--all", "--no-legend", "--plain"], | |
| capture_output=True, | |
| text=True, | |
| check=True, | |
| ) | |
| for line in result.stdout.splitlines(): | |
| if not line.strip(): | |
| continue | |
| service = line.split()[0] | |
| try: | |
| show = subprocess.run( | |
| [ | |
| "systemctl", | |
| "show", | |
| service, | |
| "-p", "Id", | |
| "-p", "ActiveState", | |
| "-p", "SubState", | |
| "-p", "ActiveEnterTimestamp", | |
| "-p", "ActiveEnterTimestampMonotonic", | |
| ], | |
| capture_output=True, | |
| text=True, | |
| check=True, | |
| ) | |
| props = {} | |
| for l in show.stdout.splitlines(): | |
| k, _, v = l.partition("=") | |
| props[k] = v | |
| services.append({ | |
| "service": props.get("Id", service), | |
| "state": props.get("ActiveState", ""), | |
| "substate": props.get("SubState", ""), | |
| "timestamp": props.get("ActiveEnterTimestamp", ""), | |
| "mono": int(props.get("ActiveEnterTimestampMonotonic", "0") or 0), | |
| }) | |
| except Exception: | |
| pass | |
| services.sort(key=lambda x: x["mono"], reverse=True) | |
| print( | |
| f"{'SERVICE':55} {'STATE':10} {'SUBSTATE':12} LAST START/RESTART" | |
| ) | |
| print("-" * 120) | |
| for s in services: | |
| ts = s["timestamp"] if s["timestamp"] else "-" | |
| print( | |
| f"{s['service'][:55]:55} " | |
| f"{s['state'][:10]:10} " | |
| f"{s['substate'][:12]:12} " | |
| f"{ts}" | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment