save point

This commit is contained in:
kake26 2025-05-12 12:37:26 -05:00
parent d0e9bd56b8
commit 8969e26033
Signed by: kake26
GPG key ID: E0A989B571D1F99F

256
svc.bash
View file

@ -1,138 +1,162 @@
#!/bin/bash #!/bin/bash
# Unified service management wrapper for systemd and OpenRC # svc.sh: Unified wrapper for OpenRC, systemd, and package management (apt, dnf, pacman)
# Function to detect init system # Exit on error
detect_init_system() { set -e
if command -v systemctl >/dev/null 2>&1 && pgrep systemd >/dev/null; then
echo "systemd" # Detect package manager
elif command -v rc-service >/dev/null 2>&1; then detect_pkg_manager() {
echo "openrc" if command -v apt >/dev/null 2>&1; then
echo "apt"
elif command -v dnf >/dev/null 2>&1; then
echo "dnf"
elif command -v pacman >/dev/null 2>&1; then
echo "pacman"
else else
echo "unknown" echo "none"
exit 1
fi fi
} }
# Function to display usage # Package management functions
usage() { pkg_install() {
echo "Usage: $0 {start|stop|restart|enable|disable|status|list} [service]" local pkg="$1"
echo "Commands:" case $(detect_pkg_manager) in
echo " start - Start a service" apt)
echo " stop - Stop a service" sudo apt install -y "$pkg"
echo " restart - Restart a service" ;;
echo " enable - Enable a service to start at boot" dnf)
echo " disable - Disable a service from starting at boot" sudo dnf install -y "$pkg"
echo " status - Check the status of a service" ;;
echo " list - List all services and their status" pacman)
exit 1 sudo pacman -S --noconfirm "$pkg"
;;
*)
echo "Error: No supported package manager found."
exit 1
;;
esac
} }
# Check for sufficient arguments pkg_remove() {
if [ $# -lt 1 ]; then local pkg="$1"
usage case $(detect_pkg_manager) in
fi apt)
sudo apt remove -y "$pkg"
;;
dnf)
sudo dnf remove -y "$pkg"
;;
pacman)
sudo pacman -R --noconfirm "$pkg"
;;
*)
echo "Error: No supported package manager found."
exit 1
;;
esac
}
COMMAND="$1" pkg_update() {
SERVICE="$2" case $(detect_pkg_manager) in
INIT_SYSTEM=$(detect_init_system) apt)
sudo apt update
;;
dnf)
sudo dnf check-update
;;
pacman)
sudo pacman -Sy
;;
*)
echo "Error: No supported package manager found."
exit 1
;;
esac
}
# Exit if init system is not supported pkg_search() {
if [ "$INIT_SYSTEM" = "unknown" ]; then local pkg="$1"
echo "Error: Unsupported init system. Only systemd and OpenRC are supported." case $(detect_pkg_manager) in
exit 1 apt)
fi apt search "$pkg"
;;
dnf)
dnf search "$pkg"
;;
pacman)
pacman -Ss "$pkg"
;;
*)
echo "Error: No supported package manager found."
exit 1
;;
esac
}
# Map unified commands to init-specific commands # Service management functions
case "$INIT_SYSTEM" in service_cmd() {
systemd) local svc="$1"
case "$COMMAND" in local action="$2"
start) if command -v systemctl >/dev/null 2>&1; then
[ -z "$SERVICE" ] && usage sudo systemctl "$action" "$svc"
systemctl start "$SERVICE" elif command -v rc-service >/dev/null 2>&1; then
sudo rc-service "$svc" "$action"
else
echo "Error: Neither systemd nor OpenRC found."
exit 1
fi
}
service_list() {
if command -v systemctl >/dev/null 2>&1; then
# List systemd services with status, using original clean logic
systemctl list-units --type=service --all --no-pager --no-legend | \
awk '$1 !~ /^●$/ && $1 != "" {if ($2 == "not-found") print $1, "not-found"; else if ($3 == "active" && $4 == "running") print $1, "running"; else if ($3 == "active" && $4 == "exited") print $1, "exited"; else print $1, "stopped"}' | \
column -t
elif command -v rc-service >/dev/null 2>&1; then
# List OpenRC services with status
for svc in $(rc-service -l); do
status=$(rc-service "$svc" status 2>/dev/null | grep -o 'started\|stopped\|crashed' || echo "unknown")
echo "$svc $status"
done | column -t
else
echo "Error: Neither systemd nor OpenRC found."
exit 1
fi
}
# Main logic
case "$1" in
pkg)
case "$2" in
install)
pkg_install "$3"
;; ;;
stop) remove)
[ -z "$SERVICE" ] && usage pkg_remove "$3"
systemctl stop "$SERVICE"
;; ;;
restart) update)
[ -z "$SERVICE" ] && usage pkg_update
systemctl restart "$SERVICE"
;; ;;
enable) search)
[ -z "$SERVICE" ] && usage pkg_search "$3"
systemctl enable "$SERVICE"
;;
disable)
[ -z "$SERVICE" ] && usage
systemctl disable "$SERVICE"
;;
status)
[ -z "$SERVICE" ] && usage
systemctl status "$SERVICE"
;;
list)
if [ -n "$SERVICE" ]; then
echo "Error: 'list' command does not take a service argument."
usage
fi
systemctl list-units --type=service --all --no-pager --no-legend | \
awk '$1 !~ /^●$/ && $1 != "" {if ($2 == "not-found") print $1, "not-found"; else if ($3 == "active" && $4 == "running") print $1, "running"; else if ($3 == "active" && $4 == "exited") print $1, "exited"; else print $1, "stopped"}' | \
column -t
;; ;;
*) *)
usage echo "Usage: svc pkg {install|remove|update|search} [package]"
exit 1
;; ;;
esac esac
;; ;;
openrc) start|stop|restart|enable|disable)
case "$COMMAND" in service_cmd "$2" "$1"
start) ;;
[ -z "$SERVICE" ] && usage list)
rc-service "$SERVICE" start service_list
;; ;;
stop) *)
[ -z "$SERVICE" ] && usage echo "Usage: svc {start|stop|restart|enable|disable} <service> | svc list | svc pkg {install|remove|update|search} [package]"
rc-service "$SERVICE" stop exit 1
;;
restart)
[ -z "$SERVICE" ] && usage
rc-service "$SERVICE" restart
;;
enable)
[ -z "$SERVICE" ] && usage
rc-update add "$SERVICE" default
;;
disable)
[ -z "$SERVICE" ] && usage
rc-update del "$SERVICE" default
;;
status)
[ -z "$SERVICE" ] && usage
rc-service "$SERVICE" status
;;
list)
if [ -n "$SERVICE" ]; then
echo "Error: 'list' command does not take a service argument."
usage
fi
for svc in $(rc-service -l); do
status=$(rc-service "$svc" status | grep -q "started" && echo "running" || echo "stopped")
echo "$svc $status"
done | column -t
;;
*)
usage
;;
esac
;; ;;
esac esac
# Check command execution status
if [ $? -eq 0 ]; then
if [ "$COMMAND" != "list" ] && [ "$COMMAND" != "status" ]; then
echo "$COMMAND on $SERVICE ($INIT_SYSTEM) completed successfully."
fi
else
echo "Error: Failed to execute $COMMAND on $SERVICE ($INIT_SYSTEM)."
exit 1
fi