This commit is contained in:
Hanashiko 2025-12-15 19:10:01 +01:00 committed by GitHub
commit c55c3fbaa1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 275 additions and 195 deletions

View file

@ -1,98 +1,96 @@
# Systemd plugin # 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) plugins=(... systemd)
``` ```
> Once added, either restart your terminal or run `source ~/.zshrc` to apply the changes.
## Aliases ## Commands and Aliases
| Alias | Command | Description | | Alias | Command | Description |
|:-----------------------|:-----------------------------------|:-----------------------------------------------------------------| |---------------|-------------|-------|
| `sc-failed` | `systemctl --failed` | List failed systemd units | | `sc` | `sudo systemctl` | System-wide systemctl |
| `sc-list-units` | `systemctl list-units` | List all units systemd has in memory | | `scr` | `sudo systemctl restart` | Restart a system-wide service |
| `sc-is-active` | `systemctl is-active` | Show whether a unit is active | | `scs` | `sudo systemctl start` | Start a system-wide service |
| `sc-status` | `systemctl status` | Show terse runtime status information about one or more units | | `sctp` | `sudo systemctl stop` | Stop a system-wide service |
| `sc-show` | `systemctl show` | Show properties of units, jobs, or the manager itself | | `scst` | `sudo systemctl status` | Check the status of a system-wide service |
| `sc-help` | `systemctl help` | Show man page of units | | `sce` | `sudo systemctl enable` | Enable a system-wide service |
| `sc-list-unit-files` | `systemctl list-unit-files` | List unit files installed on the system | | `scen` | `sudo systemctl enable --now` | Enable and start unit |
| `sc-is-enabled` | `systemctl is-enabled` | Checks whether any of the specified unit files are enabled | | `scre` | `sudo systemctl reenable` | Reenable unit |
| `sc-list-jobs` | `systemctl list-jobs` | List jobs that are in progress | | `scd` | `sudo systemctl disable` | Disable a system-wide service |
| `sc-show-environment` | `systemctl show-environment` | Dump the systemd manager environment block | | `scdn` | `sudo systemctl disable --now` | Disable and stop unit |
| `sc-cat` | `systemctl cat` | Show backing files of one or more units | | `screl` | `sudo systemctl reload` | Reload a system-wide service |
| `sc-list-timers` | `systemctl list-timers` | List timer units currently in memory | | `scres` | `sudo systemctl restart` | Restart system-wide service |
| **Aliases with sudo** ||| | `sctr` | `sudo systemctl try-restart` | Restart system-wide service |
| `sc-start` | `sudo systemctl start` | Start Unit(s) | | `scisol` | `sudo systemctl isolate` | Start a unit and its dependencies and stop all others |
| `sc-stop` | `sudo systemctl stop` | Stop Unit(s) | | `sckill` | `sudo systemctl kill` | Kill unit |
| `sc-reload` | `sudo systemctl reload` | Reload Unit(s) | | `scresfail` | `sudo systemctl reset-failed` | Reset the failed state of the specified units |
| `sc-restart` | `sudo systemctl restart` | Restart Unit(s) | | `scpres` | `sudo systemctl preset` | Reset the enable/disable status one or more unit files |
| `sc-try-restart` | `sudo systemctl try-restart` | Restart Unit(s) | | `scmask` | `sudo systemctl mask` | Mask unit |
| `sc-isolate` | `sudo systemctl isolate` | Start a unit and its dependencies and stop all others | | `scunmask` | `sudo systemctl unmask` | Unmask unit |
| `sc-kill` | `sudo systemctl kill` | Kill unit(s) | | `scmaskn` | `sudo systemctl mask --now` | Mask and stop unit |
| `sc-reset-failed` | `sudo systemctl reset-failed` | Reset the "failed" state of the specified units, | | `sclink` | `sudo systemctl link` | Link a unit file into the unit file search path |
| `sc-enable` | `sudo systemctl enable` | Enable unit(s) | | `scload` | `sudo systemctl load` | Load unit |
| `sc-disable` | `sudo systemctl disable` | Disable unit(s) | | `sccnl` | `sudo systemctl cancel` | Cancel job |
| `sc-reenable` | `sudo systemctl reenable` | Reenable unit(s) | | `scstenv` | `sudo systemctl set-environment` | Set one or more systemd manager environment variables |
| `sc-preset` | `sudo systemctl preset` | Reset the enable/disable status one or more unit files | | `scunstenv` | `sudo systemctl unset-environment` | Unset one or more systemd manager environment variables |
| `sc-mask` | `sudo systemctl mask` | Mask unit(s) | | `scedt` | `sudo systemctl edit` | Edit a drop-in snippet or a whole replacement file with --full |
| `sc-unmask` | `sudo systemctl unmask` | Unmask unit(s) | | `scia` | `systemctl is-active` | Show whether a unit is active |
| `sc-link` | `sudo systemctl link` | Link a unit file into the unit file search path | | `scie` | `systemctl is-enabled` | Checks whether any of the specified unit files are enabled |
| `sc-load` | `sudo systemctl load` | Load unit(s) | | `scsh` | `systemctl show` | Show properties of units, jobs, or the manager itself |
| `sc-cancel` | `sudo systemctl cancel` | Cancel job(s) | | `schelp` | `systemctl help` | Show man page of units |
| `sc-set-environment` | `sudo systemctl set-environment` | Set one or more systemd manager environment variables | | `scshenv` | `systemctl show-environment` | Dump the systemd manager environment block |
| `sc-unset-environment` | `sudo systemctl unset-environment` | Unset one or more systemd manager environment variables | | `sccat` | `systemctl cat` | Show backing files of one or more units |
| `sc-edit` | `sudo systemctl edit` | Edit a drop-in snippet or a whole replacement file with `--full` | | `scu` | `systemctl --user` | User-level systemctl |
| `sc-enable-now` | `sudo systemctl enable --now` | Enable and start unit(s) | | `scur` | `systemctl --user restart` | Restart a user-level service |
| `sc-disable-now` | `sudo systemctl disable --now` | Disable and stop unit(s) | | `scus` | `systemctl --user start` | Start a user-level service |
| `sc-mask-now` | `sudo systemctl mask --now` | Mask and stop unit(s) | | `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`. - `scheck`: Check the status and enablement of a service
For example: `scu-list-units` will be aliased to `systemctl --user list-units`. - Usage: `scheck <service_name>`
- Example: `scheck httpd`
- `stoggle`: Toggle a service (start if stopped, stop if started)
- Usage: `stoggle <service_name>`
- Example: `stoggle httpd`
- `srestart`: Restart a service and show its status
- Usage: `srestart <service_name`
- Example: `srestart httpd`
- `slogs`: Show the last few log entries for a service
- Usage: `slogs <service_name> [number_of_lines]`
- Example: `slogs httpd 50`
- `swatchlog`: Watch the logs of a service in real-time
- Usage: `swatchlog <service_name>`
- Example: `swatchlog httpd`
- `smulti`: Manage multiple services at once (start, stop, restart, or check status)
- Usage: `smulti [start|stop|restart|status] <service1> <service2> ...`
- Example: `smulti start httpd mariadb`
- `sfind`: Search for systemd units
- Usage: `sfind <search_pattern>`
- Example: `sfind http`
### Unit Status Prompt This plugin should help streamline your work with `systemctl` commands and save you time when managing services on your system.
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
<prefix><unit>:<active|notactive><suffix>
```
You can control these parts with the following variables:
- `<prefix>`: Set `$ZSH_THEME_SYSTEMD_PROMPT_PREFIX`.
- `<suffix>`: Set `$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX`.
- `<unit>`: 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.
- `<active>`: shown if the systemd unit is active.
Set `$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE`.
- `<notactive>`: 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]
```

View file

@ -1,121 +1,203 @@
# systemctl aliases # Systemctl plugin
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
)
sudo_commands=( # Basic service operations
add-requires alias sc='sudo systemctl'
add-wants alias scr='sudo systemctl restart'
cancel alias scs='sudo systemctl start'
daemon-reexec alias sctp='sudo systemctl stop'
daemon-reload alias scst='sudo systemctl status'
default alias sce='sudo systemctl enable'
disable alias scen='sudo systemctl enable --now'
edit alias scre='sudo systemctl reenable'
emergency alias scd='sudo systemctl disable'
enable alias scdn='sudo systemctl disable --now'
halt alias screl='sudo systemctl reload'
import-environment alias scres='sudo systemctl restart'
isolate alias sctr='sudo systemctl try-restart'
kexec alias scisol='sudo systemctl isolate'
kill alias sckill='sudo systemctl kill'
link alias scresfail='sudo systemctl reset-failed'
list-machines alias scpres='sudo systemctl preset'
load alias scmask='sudo systemctl mask'
mask alias scunmask='sudo systemctl unmask'
preset alias scmaskn='sudo systemctl mask --now'
preset-all alias sclink='sudo systemctl link'
reenable alias scload='sudo systemctl load'
reload alias sccnl='sudo systemctl cancel'
reload-or-restart alias scstenv='sudo systemctl set-environment'
reset-failed alias scunstenv='sudo systemctl unset-environment'
rescue alias scedt='sudo systemctl edit'
restart alias scia='systemctl is-active'
revert alias scie='systemctl is-enabled'
set-default alias scsh='systemctl show'
set-environment alias schelp='systemctl help'
set-property alias scshenv='systemctl show-environment'
start alias sccat='systemctl cat'
stop
switch-root
try-reload-or-restart
try-restart
unmask
unset-environment
)
power_commands=( # User-level service operations (without sudo)
hibernate alias scu='systemctl --user'
hybrid-sleep alias scur='systemctl --user restart'
poweroff alias scus='systemctl --user start'
reboot alias scup='systemctl --user stop'
suspend 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 # List services
alias "sc-$c"="systemctl $c" alias scls='systemctl list-units --type=service'
alias "scu-$c"="systemctl --user $c" alias sclsa='systemctl list-units --type=service --all'
done 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 # List timers
alias "sc-$c"="sudo systemctl $c" alias sclt='systemctl list-timers'
alias "scu-$c"="systemctl --user $c" alias sclta='systemctl list-timers --all'
done
for c in $power_commands; do # Journalctl shortcuts
alias "sc-$c"="systemctl $c" alias jc='sudo journalctl'
done 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 <service_name>"
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 <service_name>"
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 # Function to restart service and show its status
alias sc-enable-now="sc-enable --now" srestart() {
alias sc-disable-now="sc-disable --now" if [ $# -eq 0 ]; then
alias sc-mask-now="sc-mask --now" echo "Usage: srestart <service_name>"
return 1
fi
local service=$1
echo "Restarting $service..."
sudo systemctl restart "$service"
sleep 1
scheck "$service"
}
alias scu-enable-now="scu-enable --now" # Function to show the last few log entries for a service
alias scu-disable-now="scu-disable --now" slogs() {
alias scu-mask-now="scu-mask --now" if [ $# -eq 0 ]; then
echo "Usage: slogs <service_name> [number_of_lines]"
return 1
fi
local service=$1
local lines=${2:-20}
sudo journalctl -u "$service" -n "$lines" --no-pager
}
# --failed commands # Function to watch logs in real-time for a service
alias scu-failed='systemctl --user --failed' swatchlog() {
alias sc-failed='systemctl --failed' if [ $# -eq 0 ]; then
echo "Usage: swatchlog <service_name>"
return 1
fi
local service=$1
sudo journalctl -u "$service" -f
}
function systemd_prompt_info { # Function to manage multiple services at once
local unit smulti() {
for unit in "$@"; do if [ $# -lt 2 ]; then
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_PREFIX" echo "Usage: smulti [start|stop|restart|status] service1 service2 ..."
return 1
if [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]]; then fi
echo -n "${(U)unit:gs/%/%%}:"
else local action=$1
echo -n "${unit:gs/%/%%}:" shift
fi
for service in "$@"; do
if systemctl is-active "$unit" &>/dev/null; then echo "=== $service ==="
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" case "$action" in
elif systemctl --user is-active "$unit" &>/dev/null; then start)
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" sudo systemctl start "$service"
else ;;
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE" stop)
fi sudo systemctl stop "$service"
;;
echo -n "$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX" restart)
sudo systemctl restart "$service"
;;
status)
systemctl status "$service" --no-pager
;;
*)
echo "Unknown action: $action"
return 1
;;
esac
echo ""
done done
} }
# Function to search for systemd units
sfind() {
if [ $# -eq 0 ]; then
echo "Usage: sfind <search_pattern>"
return 1
fi
systemctl list-unit-files | grep -i "$1"
}