svc

Simple service scripts and examples, to be used everywhere.
git clone git://r-36.net/svc
Log | Files | Refs | LICENSE

svc (1882B)


      1 #!/bin/sh
      2 #
      3 # See LICENSE file for license details.
      4 #
      5 
      6 BASEDIR="/bin/svc.d"
      7 BASENAME="$(basename $0)"
      8 
      9 [ -d "$BASEDIR" ] || mkdir -p "$BASEDIR"
     10 cd $BASEDIR
     11 
     12 usage() {
     13 	printf "usage: %s [-acdklrs] [service]\n" "$BASENAME" >&2
     14 	exit 1
     15 }
     16 
     17 if [ $# -lt 1 ];
     18 then
     19 	usage
     20 fi
     21 
     22 runservice() {
     23 	service="$1"
     24 	dir="$2"
     25 	cmd="$3"
     26 
     27 	if [ $(stat -L -t "$dir/$service" | cut -d' ' -f 2) -eq 0 ];
     28 	then
     29 		"./bare.sh" "$service" "$cmd" 2>&1 >/dev/null &
     30 	else
     31 		"$dir/$service" "$cmd" 2>&1 >/dev/null &
     32 	fi
     33 }
     34 
     35 doservice() {
     36 	cmd="$1"
     37 	service="$2"
     38 
     39 	if [ -z "${service}" ];
     40 	then
     41 		services=$(ls -1 run)
     42 		for s in ${services};
     43 		do
     44 			runservice "$s" run "$cmd"
     45 			printf "%s: '%s' %s.\n" \
     46 				"$BASENAME" "$s" "$cmd"
     47 		done
     48 	else
     49 		if [ -x "avail/$service" ];
     50 		then	
     51 			runservice "$service" avail "$cmd"
     52 			printf "%s: '%s' %s.\n" \
     53 				"$BASENAME" "$service" "$cmd"
     54 		else
     55 			printf "%s: '%s' is not available or executable.\n" \
     56 				"$BASENAME" "$service" >&2
     57 			exit 1
     58 		fi
     59 	fi
     60 }
     61 
     62 case "$1" in
     63 	-a)
     64 		if [ $# -gt 1 ] && [ -e "avail/$2" ];
     65 		then
     66 			ln -sf "../avail/$2" "run/$2"
     67 			printf "%s: '%s' activated.\n" \
     68 				"$BASENAME" "$2"
     69 		else
     70 			cd run
     71 			ls .
     72 		fi 
     73 		;;	
     74 	-c)
     75 		[ -d "$BASEDIR" ] || mkdir -p "$BASEDIR"
     76 		[ -d "$BASEDIR/avail" ] || mkdir -p "$BASEDIR/avail"
     77 		[ -d "$BASEDIR/default" ] || mkdir -p "$BASEDIR/default"
     78 		[ -d "$BASEDIR/run" ] || mkdir -p "$BASEDIR/run"
     79 		;;
     80 	-d)
     81 		if [ $# -gt 1 ] && [ -e "run/$2" ];
     82 		then
     83 			rm -f "run/$2"
     84 			printf "%s: '%s' deactivated.\n" \
     85 				"$BASENAME" "$2"
     86 		else
     87 			printf "%s: No service given or service is not marked as to run.\n" \
     88 				"$BASENAME" >&2
     89 			exit 1
     90 		fi
     91 		;;
     92 	-k|-s)
     93 		doservice "$1" "$2"
     94 		;;
     95 	-l)
     96 		cd avail
     97 		ls .
     98 		;;
     99 	-r)
    100 		if [ $# -gt 1 ];
    101 		then
    102 			set -x
    103 			svc -k "$2"
    104 			svc -s "$2"
    105 		else
    106 			printf "%s: please give a service name.\n" \
    107 				"$BASENAME" >&2
    108 			exit 1
    109 		fi
    110 		;;
    111 	*)
    112 		usage
    113 		;;
    114 esac
    115