Blame
Date:
Sun Aug 3 20:18:56 2014 UTC
Message:
fix
001
2014-08-03
alienus
#!/bin/bash
002
2014-08-03
alienus
#
003
2014-08-03
alienus
#
004
2014-08-03
alienus
# service: Play with NuTyX services
005
2014-08-03
alienus
#
006
2014-08-03
alienus
# Based on service from Salix
007
2014-08-03
alienus
# https://github.com/gapan/salixtools/tree/master/service
008
2014-08-03
alienus
#
009
2014-08-03
alienus
#
010
2014-08-03
alienus
011
2014-08-03
alienus
SERVICE_VERSION=0.1
012
2014-08-03
alienus
013
2014-08-03
alienus
#
014
2014-08-03
alienus
# ex: service list
015
2014-08-03
alienus
# List all available services
016
2014-08-03
alienus
#
017
2014-08-03
alienus
# ex: service start cups
018
2014-08-03
alienus
# Start the cups daemon
019
2014-08-03
alienus
#
020
2014-08-03
alienus
021
2014-08-03
alienus
print_help()
022
2014-08-03
alienus
{
023
2014-08-03
alienus
echo "Usage: service [options] SERVICENAME"
024
2014-08-03
alienus
echo "Options:"
025
2014-08-03
alienus
echo " list List all available services with their status"
026
2014-08-03
alienus
echo " start Start service SERVICENAME"
027
2014-08-03
alienus
echo " stop Stop service SERVICENAME"
028
2014-08-03
alienus
echo " restart Restart service SERVICENAME"
029
2014-08-03
alienus
echo " version Print release version of this script"
030
2014-08-03
alienus
echo " help Print this help and exit"
031
2014-08-03
alienus
exit 0
032
2014-08-03
alienus
}
033
2014-08-03
alienus
034
2014-08-03
alienus
die()
035
2014-08-03
alienus
{
036
2014-08-03
alienus
echo "$@"
037
2014-08-03
alienus
exit 1
038
2014-08-03
alienus
}
039
2014-08-03
alienus
040
2014-08-03
alienus
check_root(){
041
2014-08-03
alienus
if [ "$UID" != "0" ]; then
042
2014-08-03
alienus
die "Only root can play with services, exit."
043
2014-08-03
alienus
fi
044
2014-08-03
alienus
}
045
2014-08-03
alienus
046
2014-08-03
alienus
print_version() {
047
2014-08-03
alienus
die "service $SERVICE_VERSION"
048
2014-08-03
alienus
}
049
2014-08-03
alienus
050
2014-08-03
alienus
# Services location
051
2014-08-03
alienus
INITDIR='/etc/rc.d/init.d'
052
2014-08-03
alienus
053
2014-08-03
alienus
# user choices
054
2014-08-03
alienus
ACTION=$1
055
2014-08-03
alienus
SERVICENAME=$2
056
2014-08-03
alienus
057
2014-08-03
alienus
# Some files have not to be listed
058
2014-08-03
alienus
# They are listed in this file
059
2014-08-03
alienus
BLACKLIST="/etc/blacklist.service"
060
2014-08-03
alienus
061
2014-08-03
alienus
blacklist(){
062
2014-08-03
alienus
echo $1 | grep -f $BLACKLIST
063
2014-08-03
alienus
}
064
2014-08-03
alienus
065
2014-08-03
alienus
start() {
066
2014-08-03
alienus
chmod 755 $INITDIR/$SERVICENAME
067
2014-08-03
alienus
if [ -r $INITDIR/$SERVICENAME ]; then
068
2014-08-03
alienus
/bin/sh $INITDIR/$SERVICENAME start
069
2014-08-03
alienus
else
070
2014-08-03
alienus
echo -e "No $SERVICENAME service"
071
2014-08-03
alienus
fi
072
2014-08-03
alienus
}
073
2014-08-03
alienus
074
2014-08-03
alienus
stop() {
075
2014-08-03
alienus
if [ -r $INITDIR/$SERVICENAME ]; then
076
2014-08-03
alienus
/bin/sh $INITDIR/$SERVICENAME stop
077
2014-08-03
alienus
chmod 644 $INITDIR/$SERVICENAME
078
2014-08-03
alienus
else
079
2014-08-03
alienus
echo -e "No $SERVICENAME service"
080
2014-08-03
alienus
fi
081
2014-08-03
alienus
}
082
2014-08-03
alienus
083
2014-08-03
alienus
restart() {
084
2014-08-03
alienus
if [ -r $INITDIR/$SERVICENAME ]; then
085
2014-08-03
alienus
/bin/sh $INITDIR/$SERVICENAME restart
086
2014-08-03
alienus
else
087
2014-08-03
alienus
echo -e "No $SERVICENAME service"
088
2014-08-03
alienus
fi
089
2014-08-03
alienus
}
090
2014-08-03
alienus
091
2014-08-03
alienus
list() {
092
2014-08-03
alienus
for i in $INITDIR/* ; do
093
2014-08-03
alienus
SERVICE="$(basename $i)"
094
2014-08-03
alienus
[ "$(blacklist $SERVICE)" ] && continue
095
2014-08-03
alienus
DESC="The $SERVICE service"
096
2014-08-03
alienus
if [ -x $i ]; then
097
2014-08-03
alienus
echo -e "$SERVICE ($DESC): [on]"
098
2014-08-03
alienus
else
099
2014-08-03
alienus
echo -e "$SERVICE ($DESC): [off]"
100
2014-08-03
alienus
fi
101
2014-08-03
alienus
done
102
2014-08-03
alienus
}
103
2014-08-03
alienus
104
2014-08-03
alienus
case "$ACTION" in
105
2014-08-03
alienus
'start')
106
2014-08-03
alienus
check_root
107
2014-08-03
alienus
start $SERVICENAME
108
2014-08-03
alienus
;;
109
2014-08-03
alienus
'stop')
110
2014-08-03
alienus
check_root
111
2014-08-03
alienus
stop $SERVICENAME
112
2014-08-03
alienus
;;
113
2014-08-03
alienus
'restart')
114
2014-08-03
alienus
check_root
115
2014-08-03
alienus
restart $SERVICENAME
116
2014-08-03
alienus
;;
117
2014-08-03
alienus
'list')
118
2014-08-03
alienus
list
119
2014-08-03
alienus
;;
120
2014-08-03
alienus
'version')
121
2014-08-03
alienus
print_version
122
2014-08-03
alienus
;;
123
2014-08-03
alienus
'help')
124
2014-08-03
alienus
print_help
125
2014-08-03
alienus
;;
126
2014-08-03
alienus
*)
127
2014-08-03
alienus
die "Wrong usage, see service help, aborting!"
128
2014-08-03
alienus
esac
129
2014-08-03
alienus
Frédéric Galusik