#! /bin/bash # function library for managing services on various OS # currently only Centos 6/7+ and Rocky 9 are supported # v. 1.0.4 # Function list: # k_service_stop() - function to stop the service, argument SERVICE # k_service_start() - function to start the service, argument SERVICE # k_service_restart() - function to restart the service, argument SERVICE # k_service_enable() - function to enable the service on system start, argument SERVICE # k_service_disable() - function to disable the service on system start, argument SERVICE # k_service_is_running() - function detects if service is running. Usage if k_service_is_running SERVICE; then... # k_service_is_enabled() - function detects if service is enabled to start on startup. Usage if k_service_is_enabled SERVICE; then... # Function naming rules # k__ where either object or action are not mandatory (only object or only action is necessary, could be both also) # function to stop the service, argument SERVICE k_service_stop() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service=$1 if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then /etc/init.d/$service stop else service $service stop fi elif [[ $ROCKY9 == 1 ]]; then /bin/systemctl stop $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi } # function to start the service, argument SERVICE k_service_start() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service=$1 if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then /etc/init.d/$service start else service $service start fi elif [[ $ROCKY9 == 1 ]]; then /bin/systemctl start $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi } # function to restart the service, argument SERVICE k_service_restart() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service=$1 if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then /etc/init.d/$service restart else /bin/systemctl restart $service fi elif [[ $ROCKY9 == 1 ]]; then /bin/systemctl restart $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi } # function to enable the service on system start, argument SERVICE k_service_enable() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service=$1 if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then chkconfig --levels 2345 $service on else /bin/systemctl enable $service fi elif [[ $ROCKY9 == 1 ]]; then /bin/systemctl enable $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi } # function to disable the service on system start, argument SERVICE k_service_disable() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service=$1 if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then chkconfig --levels 2345 $service off else /bin/systemctl disable $service fi elif [[ $ROCKY9 == 1 ]]; then /bin/systemctl disable $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi } # function detects if service is running. Usage if k_service_is_running SERVICE; then... k_service_is_running() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service="$1" if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then LC_ALL=C /etc/init.d/"$service" status 2>/dev/null | grep -Fq 'running' else # first we check if this service present at all (to avoid nasty message Unit ___.service could not be found.) if /bin/systemctl --all --type service | grep -q $service; then LC_ALL=C /bin/systemctl status $service | grep -q "active (running)" else return fi fi elif [[ $ROCKY9 == 1 ]]; then # first we check if this service present at all (to avoid nasty message Unit ___.service could not be found.) if /bin/systemctl --all --type service | grep -q $service; then LC_ALL=C /bin/systemctl status $service | grep -q "active (running)" else return fi else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi # k_centos_version # if ((centos_version == 6)); then # Using /etc/init.d instead of service because # It is not possible to force english in service command (it clears environment) # [root@Donald ~]# service asterisk status # asterisk (PID 24632) wird ausgeführt ... # [root@Donald ~]# LC_ALL=C service asterisk status # asterisk (PID 24632) wird ausgeführt ... # [root@Donald ~]# LC_ALL=C /etc/init.d/asterisk status # asterisk (pid 24632) is running... # LC_ALL=C /etc/init.d/"$service" status 2>/dev/null | grep -Fq 'running' # else #systemctl is-active --quiet "$service" # could be like this: #[root@DB-RADIUS m2]# service m2_server_loadstats status #FAILED m2_server_loadstats daemon is stopped #[root@DB-RADIUS m2]# #[root@DB-RADIUS framework]# systemctl is-active m2_server_loadstats #active #systemctl status m2_server_loadstats #Active: active (exited) since Tue 2020-12-29 09:18:00 UTC; 1 months 4 days ago # first we check if this service present at all (to avoid nasty message Unit ___.service could not be found.) # if systemctl --all --type service | grep -q $service; then # LC_ALL=C systemctl status $service | grep -q "active (running)" # else # return # fi # fi } k_service_is_enabled_chkcfg() { LC_ALL=C chkconfig --list $1 2>/dev/null | grep -q "3:on" } k_service_is_enabled_systemctl() { local service="$1" # first we check if this service present at all (to avoid nasty message Unit ___.service could not be found.) if /bin/systemctl --all --type service | grep -q $service; then /bin/systemctl is-enabled $service 2> /dev/null | grep -q "enabled" else return fi } # function detects if service is enabled to start on startup. Usage if k_service_is_enabled SERVICE; then... k_service_is_enabled() { if [[ $OS == "" && $DIST == "" ]]; then k_detect_os fi local service="$1" if [[ $service == "" ]]; then report "Empty service name" 1 fi if [[ $OS == "centos" ]]; then if [[ $DIST == "6" ]]; then k_service_is_enabled_chkcfg $service else k_service_is_enabled_systemctl $service fi elif [[ $ROCKY9 == 1 ]]; then k_service_is_enabled_systemctl $service else report "$OS $DIST not supported" 1 EXIT_CODE=1 fi # local service="$1" # k_centos_version # if ((centos_version == 6)); then # k_service_is_enabled_chkcfg $service # else # k_service_is_enabled_systemctl $service # fi }