~fredg/NuTyX

NuTyX/service -rwxr-xr-x 2.4 KiB
2ecaab31 — alienus fix 8 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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