feat(systemd): Improvements to the plugin. more aliases added, additional features added

This commit is contained in:
Hanashiko 2025-05-08 18:33:59 +03:00
commit d277f3f33f
2 changed files with 279 additions and 199 deletions

View file

@ -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 <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
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]
```
This plugin should help streamline your work with `systemctl` commands and save you time when managing services on your system.