svc.sh: support systemd

This commit is contained in:
mollusk 2021-10-23 20:26:23 -07:00
parent d1e9c221a3
commit 84a19253c0

142
svc.sh
View File

@ -31,6 +31,17 @@ LPURPLE="\033[1;35m"
BWHITE="\e[1m" BWHITE="\e[1m"
NC='\033[0m' # No Color NC='\033[0m' # No Color
osType=$(lsb-release -a | grep Distributor | cut -d ':' -f 2,2)
if [[ -f "/usr/bin/sv " ]];then
serviceManager="runit"
elif [[ -f "/usr/bin/systemctl" ]];then
serviceManager="systemd"
fi
help(){ help(){
printf """ printf """
svc - Service Commander - frontend for the sv command svc - Service Commander - frontend for the sv command
@ -53,49 +64,108 @@ up Show status of running services
""" """
} }
case $1 in runit(){
case $1 in
enable | --enable-service ) enable | --enable-service )
ln -v -s /etc/sv/"${2}" /var/service/ ln -v -s /etc/sv/"${2}" /var/service/
;; ;;
disable | --remove-service ) disable | --remove-service )
rm -v /var/service/"${2}" rm -v /var/service/"${2}"
;; ;;
enabled | --enabled-services ) enabled | --enabled-services )
cd /etc/sv && find -xtype l -exec ls -l {} \; cd /etc/sv && find -xtype l -exec ls -l {} \;
;; ;;
list | ls | --list ) list | ls | --list )
echo echo
echo -e "${LGREEN}Available Services (/etc/sv/):${NC}\n" echo -e "${LGREEN}Available Services (/etc/sv/):${NC}\n"
ls /etc/sv ls /etc/sv
echo echo
echo -e "${LCYAN}Enabled Services (/var/service):${NC}\n" echo -e "${LCYAN}Enabled Services (/var/service):${NC}\n"
echo echo
ls /var/service ls /var/service
echo echo
;; ;;
up ) up )
sv s /var/service/* sv s /var/service/*
;; ;;
start ) start )
sv start "${2}" sv start "${2}"
;; ;;
restart ) restart )
sv restart "${2}" sv restart "${2}"
;; ;;
stop ) stop )
sv stop "${2}" sv stop "${2}"
;; ;;
--help | -h | help ) --help | -h | help )
help help
;; ;;
esac esac
}
systemd(){
case "${1}" in
enable | --enable-service)
systemctl enable "${2}"
;;
disable | --disable-service)
systemctl disable "${2}"
;;
enabled | --enabled-services)
systemctl list-unit-files --state=enabled
;;
list | ls | --list)
echo
echo -e "${LGREEN}Available Services (/etc/systemd/system):${NC}\n"
ls /etc/systemd/system/
echo
echo -e "${LCYAN}Enabled Services:${NC}\n"
systemctl list-unit-files --state=enabled
echo
;;
up)
systemctl --type=service --state=running
;;
start)
systemctl start "${2}"
;;
restart)
systemctl restart "${2}"
;;
stop)
systemctl stop "${2}"
;;
--help | -help | help)
help
;;
esac
}
if [[ "${serviceManager}" = "runit" ]];then
runit "${1}" "${2}"
elif [[ "${serviceManager}" ]];then
systemd "${1}" "${2}"
fi