From d277f3f33f0ebfdf7b216b9880a78abf40edddd7 Mon Sep 17 00:00:00 2001 From: Hanashiko Date: Thu, 8 May 2025 18:33:59 +0300 Subject: [PATCH] feat(systemd): Improvements to the plugin. more aliases added, additional features added --- plugins/systemd/README.md | 172 ++++++++--------- plugins/systemd/systemd.plugin.zsh | 298 ++++++++++++++++++----------- 2 files changed, 275 insertions(+), 195 deletions(-) diff --git a/plugins/systemd/README.md b/plugins/systemd/README.md index 755c649a1..b0b1b32a3 100644 --- a/plugins/systemd/README.md +++ b/plugins/systemd/README.md @@ -1,98 +1,96 @@ # Systemd plugin -The systemd plugin provides many useful aliases for systemd. +This plugin provides easy aliases and functions for working with `systemctl` commands, making service management more convenient. It includes both system-wide and user-level service commands, as well as functions for toggling, restarting, and checking service statuses. -To use it, add systemd to the plugins array of your zshrc file: +## Installation -```zsh +To use this plugin, add `systemd` to the plugins array in your `.zshrc` file: + +``` plugins=(... systemd) ``` + > Once added, either restart your terminal or run `source ~/.zshrc` to apply the changes. -## Aliases +## Commands and Aliases -| Alias | Command | Description | -|:-----------------------|:-----------------------------------|:-----------------------------------------------------------------| -| `sc-failed` | `systemctl --failed` | List failed systemd units | -| `sc-list-units` | `systemctl list-units` | List all units systemd has in memory | -| `sc-is-active` | `systemctl is-active` | Show whether a unit is active | -| `sc-status` | `systemctl status` | Show terse runtime status information about one or more units | -| `sc-show` | `systemctl show` | Show properties of units, jobs, or the manager itself | -| `sc-help` | `systemctl help` | Show man page of units | -| `sc-list-unit-files` | `systemctl list-unit-files` | List unit files installed on the system | -| `sc-is-enabled` | `systemctl is-enabled` | Checks whether any of the specified unit files are enabled | -| `sc-list-jobs` | `systemctl list-jobs` | List jobs that are in progress | -| `sc-show-environment` | `systemctl show-environment` | Dump the systemd manager environment block | -| `sc-cat` | `systemctl cat` | Show backing files of one or more units | -| `sc-list-timers` | `systemctl list-timers` | List timer units currently in memory | -| **Aliases with sudo** ||| -| `sc-start` | `sudo systemctl start` | Start Unit(s) | -| `sc-stop` | `sudo systemctl stop` | Stop Unit(s) | -| `sc-reload` | `sudo systemctl reload` | Reload Unit(s) | -| `sc-restart` | `sudo systemctl restart` | Restart Unit(s) | -| `sc-try-restart` | `sudo systemctl try-restart` | Restart Unit(s) | -| `sc-isolate` | `sudo systemctl isolate` | Start a unit and its dependencies and stop all others | -| `sc-kill` | `sudo systemctl kill` | Kill unit(s) | -| `sc-reset-failed` | `sudo systemctl reset-failed` | Reset the "failed" state of the specified units, | -| `sc-enable` | `sudo systemctl enable` | Enable unit(s) | -| `sc-disable` | `sudo systemctl disable` | Disable unit(s) | -| `sc-reenable` | `sudo systemctl reenable` | Reenable unit(s) | -| `sc-preset` | `sudo systemctl preset` | Reset the enable/disable status one or more unit files | -| `sc-mask` | `sudo systemctl mask` | Mask unit(s) | -| `sc-unmask` | `sudo systemctl unmask` | Unmask unit(s) | -| `sc-link` | `sudo systemctl link` | Link a unit file into the unit file search path | -| `sc-load` | `sudo systemctl load` | Load unit(s) | -| `sc-cancel` | `sudo systemctl cancel` | Cancel job(s) | -| `sc-set-environment` | `sudo systemctl set-environment` | Set one or more systemd manager environment variables | -| `sc-unset-environment` | `sudo systemctl unset-environment` | Unset one or more systemd manager environment variables | -| `sc-edit` | `sudo systemctl edit` | Edit a drop-in snippet or a whole replacement file with `--full` | -| `sc-enable-now` | `sudo systemctl enable --now` | Enable and start unit(s) | -| `sc-disable-now` | `sudo systemctl disable --now` | Disable and stop unit(s) | -| `sc-mask-now` | `sudo systemctl mask --now` | Mask and stop unit(s) | +| Alias | Command | Description | +|---------------|-------------|-------| +| `sc` | `sudo systemctl` | System-wide systemctl | +| `scr` | `sudo systemctl restart` | Restart a system-wide service | +| `scs` | `sudo systemctl start` | Start a system-wide service | +| `sctp` | `sudo systemctl stop` | Stop a system-wide service | +| `scst` | `sudo systemctl status` | Check the status of a system-wide service | +| `sce` | `sudo systemctl enable` | Enable a system-wide service | +| `scen` | `sudo systemctl enable --now` | Enable and start unit | +| `scre` | `sudo systemctl reenable` | Reenable unit | +| `scd` | `sudo systemctl disable` | Disable a system-wide service | +| `scdn` | `sudo systemctl disable --now` | Disable and stop unit | +| `screl` | `sudo systemctl reload` | Reload a system-wide service | +| `scres` | `sudo systemctl restart` | Restart system-wide service | +| `sctr` | `sudo systemctl try-restart` | Restart system-wide service | +| `scisol` | `sudo systemctl isolate` | Start a unit and its dependencies and stop all others | +| `sckill` | `sudo systemctl kill` | Kill unit | +| `scresfail` | `sudo systemctl reset-failed` | Reset the failed state of the specified units | +| `scpres` | `sudo systemctl preset` | Reset the enable/disable status one or more unit files | +| `scmask` | `sudo systemctl mask` | Mask unit | +| `scunmask` | `sudo systemctl unmask` | Unmask unit | +| `scmaskn` | `sudo systemctl mask --now` | Mask and stop unit | +| `sclink` | `sudo systemctl link` | Link a unit file into the unit file search path | +| `scload` | `sudo systemctl load` | Load unit | +| `sccnl` | `sudo systemctl cancel` | Cancel job | +| `scstenv` | `sudo systemctl set-environment` | Set one or more systemd manager environment variables | +| `scunstenv` | `sudo systemctl unset-environment` | Unset one or more systemd manager environment variables | +| `scedt` | `sudo systemctl edit` | Edit a drop-in snippet or a whole replacement file with --full | +| `scia` | `systemctl is-active` | Show whether a unit is active | +| `scie` | `systemctl is-enabled` | Checks whether any of the specified unit files are enabled | +| `scsh` | `systemctl show` | Show properties of units, jobs, or the manager itself | +| `schelp` | `systemctl help` | Show man page of units | +| `scshenv` | `systemctl show-environment` | Dump the systemd manager environment block | +| `sccat` | `systemctl cat` | Show backing files of one or more units | +| `scu` | `systemctl --user` | User-level systemctl | +| `scur` | `systemctl --user restart` | Restart a user-level service | +| `scus` | `systemctl --user start` | Start a user-level service | +| `scup` | `systemctl --user stop` | Stop a user-level service | +| `scust` | `systemctl --user status` | Check the status of a user-level service | +| `scue` | `systemctl --user enable` | Enable a user-level service | +| `scud` | `systemctl --user disable` | Disable a user-level service | +| `scure` | `systemctl --use reload` | Reload a user-level service | +| `scls` | `systemctl list-units --type=service` | List active services | +| `sclsa` | `systemctl list-units --type=service --all` | List all services | +| `sclsf` | `systemctl list-units --type=service --failed` | List failed services | +| `sclsr` | `systemctl list-units --type=service --state=runnings` | List running services | +| `sclf` | `systemctl list-unit-files` | List unit files installed on the system | +| `sclj` | `systemctl list-jobs` | List jobs that are in progress | +| `sclt` | `systemctl list-timers` | List active timers | +| `sclta` | `systemctl list-timers --all` | List all timers | +| `jc` | `sudo journalctl` | View system logs | +| `jcf` | `sudo journalctl -f` | Follow system logs | +| `jcb` | `sudo journalctl -b` | View the logs from the current boot | +| `jcl` | `sudo journalctl --since "1 hour ago"` | View logs from the past hour | +| `jcu` | `sudo journalctl -u` | View logs for a specific service | -### User aliases +## Functions -You can use the above aliases as `--user` by using the prefix `scu` instead of `sc`. -For example: `scu-list-units` will be aliased to `systemctl --user list-units`. + - `scheck`: Check the status and enablement of a service + - Usage: `scheck ` + - Example: `scheck httpd` + - `stoggle`: Toggle a service (start if stopped, stop if started) + - Usage: `stoggle ` + - Example: `stoggle httpd` + - `srestart`: Restart a service and show its status + - Usage: `srestart [number_of_lines]` + - Example: `slogs httpd 50` + - `swatchlog`: Watch the logs of a service in real-time + - Usage: `swatchlog ` + - Example: `swatchlog httpd` + - `smulti`: Manage multiple services at once (start, stop, restart, or check status) + - Usage: `smulti [start|stop|restart|status] ...` + - Example: `smulti start httpd mariadb` + - `sfind`: Search for systemd units + - Usage: `sfind ` + - Example: `sfind http` -### Unit Status Prompt - -You can add a token to your prompt in a similar way to the gitfast plugin. To add the token -to your prompt, drop `$(systemd_prompt_info [unit]...)` into your prompt (more than one unit -may be specified). - -The plugin will add the following to your prompt for each `$unit`. - -```text -: -``` - -You can control these parts with the following variables: - -- ``: Set `$ZSH_THEME_SYSTEMD_PROMPT_PREFIX`. - -- ``: Set `$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX`. - -- ``: name passed as parameter to the function. If you want it to be in ALL CAPS, - you can set the variable `$ZSH_THEME_SYSTEMD_PROMPT_CAPS` to a non-empty string. - -- ``: shown if the systemd unit is active. - Set `$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE`. - -- ``: shown if the systemd unit is *not* active. - Set `$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE`. - -For example, if your prompt contains `PROMPT='$(systemd_prompt_info dhcpd httpd)'` and you set the following variables: - -```sh -ZSH_THEME_SYSTEMD_PROMPT_PREFIX="[" -ZSH_THEME_SYSTEMD_PROMPT_SUFFIX="]" -ZSH_THEME_SYSTEMD_PROMPT_ACTIVE="+" -ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE="X" -ZSH_THEME_SYSTEMD_PROMPT_CAPS=1 -``` - -If `dhcpd` is running, and `httpd` is not, then your prompt will look like this: - -```text -[DHCPD: +][HTTPD: X] -``` +This plugin should help streamline your work with `systemctl` commands and save you time when managing services on your system. diff --git a/plugins/systemd/systemd.plugin.zsh b/plugins/systemd/systemd.plugin.zsh index 382a57b29..5165a763e 100644 --- a/plugins/systemd/systemd.plugin.zsh +++ b/plugins/systemd/systemd.plugin.zsh @@ -1,121 +1,203 @@ -# systemctl aliases -user_commands=( - cat - get-default - help - is-active - is-enabled - is-failed - is-system-running - list-dependencies - list-jobs - list-sockets - list-timers - list-unit-files - list-units - show - show-environment - status -) +# Systemctl plugin -sudo_commands=( - add-requires - add-wants - cancel - daemon-reexec - daemon-reload - default - disable - edit - emergency - enable - halt - import-environment - isolate - kexec - kill - link - list-machines - load - mask - preset - preset-all - reenable - reload - reload-or-restart - reset-failed - rescue - restart - revert - set-default - set-environment - set-property - start - stop - switch-root - try-reload-or-restart - try-restart - unmask - unset-environment -) +# Basic service operations +alias sc='sudo systemctl' +alias scr='sudo systemctl restart' +alias scs='sudo systemctl start' +alias sctp='sudo systemctl stop' +alias scst='sudo systemctl status' +alias sce='sudo systemctl enable' +alias scen='sudo systemctl enable --now' +alias scre='sudo systemctl reenable' +alias scd='sudo systemctl disable' +alias scdn='sudo systemctl disable --now' +alias screl='sudo systemctl reload' +alias scres='sudo systemctl restart' +alias sctr='sudo systemctl try-restart' +alias scisol='sudo systemctl isolate' +alias sckill='sudo systemctl kill' +alias scresfail='sudo systemctl reset-failed' +alias scpres='sudo systemctl preset' +alias scmask='sudo systemctl mask' +alias scunmask='sudo systemctl unmask' +alias scmaskn='sudo systemctl mask --now' +alias sclink='sudo systemctl link' +alias scload='sudo systemctl load' +alias sccnl='sudo systemctl cancel' +alias scstenv='sudo systemctl set-environment' +alias scunstenv='sudo systemctl unset-environment' +alias scedt='sudo systemctl edit' +alias scia='systemctl is-active' +alias scie='systemctl is-enabled' +alias scsh='systemctl show' +alias schelp='systemctl help' +alias scshenv='systemctl show-environment' +alias sccat='systemctl cat' -power_commands=( - hibernate - hybrid-sleep - poweroff - reboot - suspend -) +# User-level service operations (without sudo) +alias scu='systemctl --user' +alias scur='systemctl --user restart' +alias scus='systemctl --user start' +alias scup='systemctl --user stop' +alias scust='systemctl --user status' +alias scue='systemctl --user enable' +alias scud='systemctl --user disable' +alias scure='systemctl --user reload' -for c in $user_commands; do - alias "sc-$c"="systemctl $c" - alias "scu-$c"="systemctl --user $c" -done +# List services +alias scls='systemctl list-units --type=service' +alias sclsa='systemctl list-units --type=service --all' +alias sclsf='systemctl list-units --type=service --failed' +alias sclsr='systemctl list-units --type=service --state=running' +alias sclf='systemctl list-unit-files' +alias sclj='systemctl list-jobs' -for c in $sudo_commands; do - alias "sc-$c"="sudo systemctl $c" - alias "scu-$c"="systemctl --user $c" -done +# List timers +alias sclt='systemctl list-timers' +alias sclta='systemctl list-timers --all' -for c in $power_commands; do - alias "sc-$c"="systemctl $c" -done +# Journalctl shortcuts +alias jc='sudo journalctl' +alias jcf='sudo journalctl -f' +alias jcb='sudo journalctl -b' +alias jcl='sudo journalctl --since "1 hour ago"' +alias jcu='sudo journalctl -u' -unset c user_commands sudo_commands power_commands +# Function to check service status with concise output +scheck() { + if [ $# -eq 0 ]; then + echo "Usage: scheck " + return 1 + fi + + local service=$1 + + if ! systemctl list-unit-files --type=service | grep -q "$service"; then + echo "Service $service not found!" + return 1 + fi + + local svc_status=$(systemctl is-active "$service") + local svc_enabled=$(systemctl is-enabled "$service" 2>/dev/null || echo "disabled") + + echo -n "Service $service is " + if [ "$svc_status" = "active" ]; then + echo -n "\033[32m$svc_status\033[0m" + else + echo -n "\033[31m$svc_status\033[0m" + fi + + echo -n " and " + if [ "$svc_enabled" = "enabled" ]; then + echo "\033[32m$svc_enabled\033[0m" + else + echo "\033[33m$svc_enabled\033[0m" + fi +} +# Function to toggle service state (start if stopped, stop if started) +stoggle() { + if [ $# -eq 0 ]; then + echo "Usage: stoggle " + return 1 + fi + + local service=$1 + + if systemctl is-active "$service" >/dev/null 2>&1; then + echo "Stopping $service..." + sudo systemctl stop "$service" + else + echo "Starting $service..." + sudo systemctl start "$service" + fi + + sleep 1 + + scheck "$service" +} -# --now commands -alias sc-enable-now="sc-enable --now" -alias sc-disable-now="sc-disable --now" -alias sc-mask-now="sc-mask --now" +# Function to restart service and show its status +srestart() { + if [ $# -eq 0 ]; then + echo "Usage: srestart " + return 1 + fi + + local service=$1 + + echo "Restarting $service..." + sudo systemctl restart "$service" + sleep 1 + + scheck "$service" +} -alias scu-enable-now="scu-enable --now" -alias scu-disable-now="scu-disable --now" -alias scu-mask-now="scu-mask --now" +# Function to show the last few log entries for a service +slogs() { + if [ $# -eq 0 ]; then + echo "Usage: slogs [number_of_lines]" + return 1 + fi + + local service=$1 + local lines=${2:-20} + + sudo journalctl -u "$service" -n "$lines" --no-pager +} -# --failed commands -alias scu-failed='systemctl --user --failed' -alias sc-failed='systemctl --failed' +# Function to watch logs in real-time for a service +swatchlog() { + if [ $# -eq 0 ]; then + echo "Usage: swatchlog " + return 1 + fi + + local service=$1 + + sudo journalctl -u "$service" -f +} -function systemd_prompt_info { - local unit - for unit in "$@"; do - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_PREFIX" - - if [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]]; then - echo -n "${(U)unit:gs/%/%%}:" - else - echo -n "${unit:gs/%/%%}:" - fi - - if systemctl is-active "$unit" &>/dev/null; then - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" - elif systemctl --user is-active "$unit" &>/dev/null; then - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" - else - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE" - fi - - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX" +# Function to manage multiple services at once +smulti() { + if [ $# -lt 2 ]; then + echo "Usage: smulti [start|stop|restart|status] service1 service2 ..." + return 1 + fi + + local action=$1 + shift + + for service in "$@"; do + echo "=== $service ===" + case "$action" in + start) + sudo systemctl start "$service" + ;; + stop) + sudo systemctl stop "$service" + ;; + restart) + sudo systemctl restart "$service" + ;; + status) + systemctl status "$service" --no-pager + ;; + *) + echo "Unknown action: $action" + return 1 + ;; + esac + echo "" done } + +# Function to search for systemd units +sfind() { + if [ $# -eq 0 ]; then + echo "Usage: sfind " + return 1 + fi + + systemctl list-unit-files | grep -i "$1" +}