~fredg/NuTyX

a48c7c32b6afa81db7aeefabbc9d611d9e9d9416 — alienus 8 years ago c1aa4da
Service script to play with NuTyX service
1 files changed, 129 insertions(+), 0 deletions(-)

A service
A service => service +129 -0
@@ 0,0 1,129 @@
#!/bin/bash
#
#
# service: Play with NuTyX services
#
# Based on service from Salix
# https://github.com/gapan/salixtools/tree/master/service
#
#

SERVICE_VERSION=0.1

#
# ex: service list
# List all available services
#
# ex: service start cups
# Start the cups daemon
#

print_help()
{
    echo "Usage: service [options] SERVICENAME"
    echo "Options:"
    echo "  list     List all available services with their status"
    echo "  start    Start service SERVICENAME"
    echo "  stop     Stop service SERVICENAME"
    echo "  restart  Restart service SERVICENAME"
    echo "  version  Print release version of this script"
    echo "  help     Print this help and exit"
    exit 0
}

die()
{
    echo "$@"
    exit 1
}

check_root(){
    if [ "$UID" != "0" ]; then
        die "Only root can play with services, exit."
    fi
}

print_version() {
    die "service $SERVICE_VERSION"
}

# Services location
INITDIR='/etc/rc.d/init.d'

# user choices
ACTION=$1
SERVICENAME=$2

# Some files have not to be listed
# They are listed in this file
BLACKLIST="/etc/blacklist.service"

blacklist(){
    echo $1 | grep -f ./$BLACKLIST
}

start() {
    chmod 755 $INITDIR/$SERVICENAME
    if [ -r $INITDIR/$SERVICENAME ]; then
        /bin/sh $INITDIR/$SERVICENAME start
    else
        echo -e "No $SERVICENAME service"
    fi
}

stop() {
    if [ -r $INITDIR/$SERVICENAME ]; then
        /bin/sh $INITDIR/$SERVICENAME stop
        chmod 644 $INITDIR/$SERVICENAME
    else
        echo -e "No $SERVICENAME service"
    fi
}

restart() {
    if [ -r $INITDIR/$SERVICENAME ]; then
        /bin/sh $INITDIR/$SERVICENAME restart
    else
        echo -e "No $SERVICENAME service"
    fi
}

list() {
    for i in $INITDIR/* ; do
        SERVICE="$(basename $i)"
        [ "$(blacklist $SERVICE)" ] && continue
        DESC="The $SERVICE service"
        if [ -x $i ]; then
            echo -e "$SERVICE ($DESC): [on]"
        else
            echo -e "$SERVICE ($DESC): [off]"
        fi
    done
}

case "$ACTION" in
    'start')
        check_root
        start $SERVICENAME
        ;;
    'stop')
        check_root
        stop $SERVICENAME
        ;;
    'restart')
        check_root
        restart $SERVICENAME
        ;;
    'list')
        list
        ;;
    'version')
        print_version
        ;;
    'help')
        print_help
        ;;
    *)
        die "Wrong usage, see service help, aborting!"
esac