fs

morpheus filesystem files
git clone git://git.2f30.org/fs
Log | Files | Refs

svc (3455B)


      1 #!/bin/sh
      2 #
      3 #MIT/X Consortium License
      4 #
      5 #© 2012-14 Christoph Lohmann <20h@r-36.net>
      6 #
      7 #Permission is hereby granted, free of charge, to any person obtaining a
      8 #copy of this software and associated documentation files (the "Software"),
      9 #to deal in the Software without restriction, including without limitation
     10 #the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11 #and/or sell copies of the Software, and to permit persons to whom the
     12 #Software is furnished to do so, subject to the following conditions:
     13 #
     14 #The above copyright notice and this permission notice shall be included in
     15 #all copies or substantial portions of the Software.
     16 #
     17 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18 #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20 #THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22 #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23 #DEALINGS IN THE SOFTWARE.
     24 #
     25 #	% svc -a	# list all links in /bin/svc.d/run (all activated services)
     26 #	% svc -a ser	# activate service to be run on startup
     27 #	% svc -c	# create the basic directories of svc (configure)
     28 #	% svc -d ser	# deactivate service
     29 #	% svc -k	# kill all services linked to /bin/svc.d/run
     30 #	% svc -k ser	# kill service
     31 #	% svc -s	# run all services linked to /bin/svc.d/run
     32 #	% svc -s ser	# run service
     33 #	% svc -l	# list all services in /bin/svc.d/avail
     34 #	% svc -r ser	# restart service
     35 
     36 PATH=/bin
     37 export PATH
     38 
     39 BASEDIR="/bin/svc.d"
     40 BASENAME="$(basename $0)"
     41 
     42 [ -d "$BASEDIR" ] || mkdir -p "$BASEDIR"
     43 cd $BASEDIR
     44 
     45 usage() {
     46 	printf "usage: %s [-acdklrs] [service]\n" "$BASENAME" >&2
     47 	exit 1
     48 }
     49 
     50 if [ $# -lt 1 ];
     51 then
     52 	usage
     53 fi
     54 
     55 runservice() {
     56 	service="$1"
     57 	dir="$2"
     58 	cmd="$3"
     59 
     60 	if [ $(stat -L -t "$dir/$service" | cut -d ' ' -f 2) -eq 0 ];
     61 	then
     62 		"./bare.sh" "$service" "$cmd" 2>&1 >/dev/null &
     63 	else
     64 		"$dir/$service" "$cmd" 2>&1 >/dev/null &
     65 	fi
     66 }
     67 
     68 doservice() {
     69 	cmd="$1"
     70 	service="$2"
     71 
     72 	if [ -z "${service}" ];
     73 	then
     74 		services=$(ls run)
     75 		for s in ${services};
     76 		do
     77 			runservice "$s" run "$cmd"
     78 			printf "%s: '%s' %s.\n" \
     79 				"$BASENAME" "$s" "$cmd"
     80 		done
     81 	else
     82 		if [ -x "avail/$service" ];
     83 		then	
     84 			runservice "$service" avail "$cmd"
     85 			printf "%s: '%s' %s.\n" \
     86 				"$BASENAME" "$service" "$cmd"
     87 		else
     88 			printf "%s: '%s' is not available or executable.\n" \
     89 				"$BASENAME" "$service" >&2
     90 			exit 1
     91 		fi
     92 	fi
     93 }
     94 
     95 case "$1" in
     96 	-a)
     97 		if [ $# -gt 1 ] && [ -e "avail/$2" ];
     98 		then
     99 			ln -sf "../avail/$2" "run/$2"
    100 			printf "%s: '%s' activated.\n" \
    101 				"$BASENAME" "$2"
    102 		else
    103 			cd run
    104 			ls .
    105 		fi 
    106 		;;	
    107 	-c)
    108 		[ -d "$BASEDIR" ] || mkdir -p "$BASEDIR"
    109 		[ -d "$BASEDIR/avail" ] || mkdir -p "$BASEDIR/avail"
    110 		[ -d "$BASEDIR/default" ] || mkdir -p "$BASEDIR/default"
    111 		[ -d "$BASEDIR/run" ] || mkdir -p "$BASEDIR/run"
    112 		;;
    113 	-d)
    114 		if [ $# -gt 1 ] && [ -e "run/$2" ];
    115 		then
    116 			rm -f "run/$2"
    117 			printf "%s: '%s' deactivated.\n" \
    118 				"$BASENAME" "$2"
    119 		else
    120 			printf "%s: No service given or service is not marked as to run.\n" \
    121 				"$BASENAME" >&2
    122 			exit 1
    123 		fi
    124 		;;
    125 	-k|-s)
    126 		doservice "$1" "$2"
    127 		;;
    128 	-l)
    129 		cd avail
    130 		ls .
    131 		;;
    132 	-r)
    133 		if [ $# -gt 1 ];
    134 		then
    135 			set -x
    136 			svc -k "$2"
    137 			svc -s "$2"
    138 		else
    139 			printf "%s: please give a service name.\n" \
    140 				"$BASENAME" >&2
    141 			exit 1
    142 		fi
    143 		;;
    144 	*)
    145 		usage
    146 		;;
    147 esac