aboutsummaryrefslogtreecommitdiff
path: root/local/sbin/user-service.sh
blob: 632272ce5fb13473629749893ae20d0e23e0d1b9 (plain)
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
#!/bin/sh
set -e

[ -z "$1" ] && {echo "Run this script only from user-service file!" && exit 1}

# Name of service
NAME="$(basename "$1")"
SERVICE="$1"

# Source input file
. "$1"
shift

OP="status"
Q=true
# Parse arguments
while [ -n "$1" ]; do
	case "$1" in
		-h|--help)
			echo "User service: $NAME"
			echo " $description"
			echo "$SERVICE [OPTION]... OPERATION"
			echo " Options:"
			echo "  -q - be quiet"
			echo " Operations:"
			echo "  status - show status of service"
			echo "  start - start service"
			echo "  stop - stop service"
			echo "  restart - restart service"
			echo "  ifrestart - restart service if it's running"
			;;
		-q)
			Q=false
			;;
		status|start|stop|restart)
			OP="$1"
			;;
		*)
			echo "Unknown argument: $1"
			exit 1
			;;
	esac
	shift
done

case "$OP" in
	status)
		if status; then
			$Q && echo "Service $NAME is running"
			exit 0
		else
			$Q && echo "Service $NAME is not running"
			exit 1
		fi
		;;
	start)
		$Q && echo -n "Starting service $NAME..."
		if start; then
			$Q && echo "	ok"
		else
			$Q && echo "	fail"
			exit 1
		fi
		;;
	stop)
		$Q && echo -n "Stopping service $NAME..."
		if stop; then
			$Q && echo "	ok"
		else
			$Q && echo "	fail"
			exit 1
		fi
		;;
	restart)
		$Q && echo "Restarting service $NAME..."
		if ! stop; then
			$Q && echo "	stop failed"
			exit 1
		fi
		if start; then
			$Q && echo "	ok"
		else
			$Q && echo "	start failed"
			exit 1
		fi
		;;
	ifrestart)
		$Q && echo "Restarting service $NAME..."
		if status; then
			if ! stop; then
				$Q && echo "	stop failed"
				exit 1
			fi
			if start; then
				$Q && echo "	ok"
			else
				$Q && echo "	start failed"
				exit 1
			fi
		fi
		;;
	*)
		echo "Invalid operation!"
		exit 3
		;;
esac