jitty-scripts/svc.sh

170 lines
3.4 KiB
Bash
Raw Normal View History

2015-08-08 21:04:55 -07:00
#!/bin/bash
# SVC - SV Commander - frontend for the sv command
2015-08-08 21:04:55 -07:00
###############################################################################################
## Copyright (c) 2015, Justin Moore
##
## Permission to use, copy, modify, and/or distribute this software
## for any purpose with or without fee is hereby granted,
2015-08-08 21:04:55 -07:00
## provided that the above copyright notice and this permission notice appear in all copies.
##
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
## AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
## OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
## WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
2015-08-08 21:04:55 -07:00
## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
##############################################################################################
2019-11-30 23:17:32 -07:00
2019-11-10 06:18:50 -07:00
RED='\033[0;31m'
LRED="\033[1;31m"
BLUE="\033[0;34m"
LBLUE="\033[1;34m"
GREEN="\033[0;32m"
LGREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[0;36m"
LCYAN="\033[1;36m"
PURPLE="\033[0;35m"
LPURPLE="\033[1;35m"
BWHITE="\e[1m"
NC='\033[0m' # No Color
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
if [[ -f "/usr/bin/sv " ]];then
serviceManager="runit"
elif [[ -f "/usr/bin/systemctl" ]];then
serviceManager="systemd"
fi
2019-11-30 23:17:32 -07:00
help(){
printf """
svc - Service Commander - frontend for the sv command
2020-02-28 12:58:55 -07:00
Usage: svc enable | disable | enabled | list | up | start | restart | stop <service>
2019-11-30 23:17:32 -07:00
For detailed help, see the commands below:
start Start an enabled service
restart Restart a enabled service
stop Stop an enabled service
enable Symenlink directory/file to /var/service
2020-02-28 12:58:55 -07:00
disable Remove symlink from /var/service
enabled View synlinked paths of all services
ls List enabled and unenabled service files
up Show status of running services
2019-11-30 23:17:32 -07:00
"""
}
2021-10-23 20:26:23 -07:00
runit(){
case $1 in
enable | --enable-service )
ln -v -s /etc/sv/"${2}" /var/service/
;;
disable | --remove-service )
rm -v /var/service/"${2}"
;;
enabled | --enabled-services )
cd /etc/sv && find -xtype l -exec ls -l {} \;
;;
list | ls | --list )
echo
echo -e "${LGREEN}Available Services (/etc/sv/):${NC}\n"
ls /etc/sv
echo
echo -e "${LCYAN}Enabled Services (/var/service):${NC}\n"
echo
ls /var/service
echo
;;
up )
sv s /var/service/*
;;
start )
sv start "${2}"
;;
restart )
sv restart "${2}"
;;
stop )
sv stop "${2}"
;;
--help | -h | help )
help
;;
esac
}
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
systemd(){
2021-10-23 20:26:23 -07:00
case "${1}" in
enable | --enable-service)
systemctl enable "${2}"
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
disable | --disable-service)
systemctl disable "${2}"
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
enabled | --enabled-services)
systemctl list-unit-files --state=enabled
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
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
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
start)
systemctl start "${2}"
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
restart)
systemctl restart "${2}"
;;
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
stop)
systemctl stop "${2}"
;;
--help | -help | help)
help
;;
esac
}
2015-08-08 21:04:55 -07:00
2021-10-23 20:26:23 -07:00
if [[ "${serviceManager}" = "runit" ]];then
runit "${1}" "${2}"
2021-10-26 20:52:26 -07:00
elif [[ "${serviceManager}" = " systemd" ]];then
2021-10-23 20:26:23 -07:00
systemd "${1}" "${2}"
fi