-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert-nginx.init
174 lines (146 loc) · 2.67 KB
/
cert-nginx.init
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# description: cert-nginx
set -o pipefail
script_name=`basename "$0"`
script_abs_name=`readlink -f "$0"`
script_path=`dirname "$script_abs_name"`
# get nginx
which nginx >/dev/null
if [ $? -ne 0 ]
then
echo "nginx not installed"
exit 1
fi
desc=cert-nginx
prog=nginx
pid_file=$script_path/work/${desc}.pid
work_dir=$script_path/work/
config_file=$script_path/cert-nginx.conf
args=(-c "$config_file" -p "$work_dir")
wait_tick=0.1
wait_tick_count=500
usage()
{
printf "usage: $script_name [start|stop|restart|status]\n"
exit 1
}
show_error()
{
local message=$1
printf "\033[;31m%s\033[0m\n" "$message"
}
show_success()
{
local message=$1
printf "\033[;32m%s\033[0m\n" "$message"
}
do_start()
{
local i
printf "[starting $desc]"
pid=`cat "$pid_file" 2>/dev/null`
if [ $? -eq 0 ]
then
kill -0 "$pid" 2>/dev/null
if [ $? -eq 0 ]
then
show_error '[already started]'
return 1
fi
fi
"$prog" "${args[@]}"
for (( i = 0; i < "$wait_tick_count"; ++i ))
do
pid=`cat "$pid_file" 2>/dev/null`
if [ $? -eq 0 ]
then
kill -0 "$pid" 2>/dev/null
if [ $? -eq 0 ]
then
show_success '[success]'
return 0
fi
fi
sleep "$wait_tick"
done
show_error '[failed]'
return 1
}
do_stop()
{
local i
printf "[stopping $desc]"
pid=`cat "$pid_file" 2>/dev/null`
if [ $? -ne 0 ]
then
show_error '[not started]'
return 1
fi
kill -0 "$pid" 2>/dev/null
if [ $? -ne 0 ]
then
show_error '[not started]'
return 1
fi
kill "$pid"
for (( i = 0; i < "$wait_tick_count"; ++i ))
do
kill -0 "$pid" 2>/dev/null
if [ $? -ne 0 ]
then
show_success '[success]'
return 0
fi
sleep "$wait_tick"
done
show_error '[failed]'
return 1
}
do_status()
{
printf "[$desc]"
pid=`cat "$pid_file" 2>/dev/null`
if [ $? -ne 0 ]
then
show_error '[stopped]'
return 0
fi
kill -0 "$pid" 2>/dev/null
if [ $? -ne 0 ]
then
show_error '[stopped]'
else
show_success '[running]'
fi
return 0
}
# main
if [ $# -ne 1 ]
then
usage
fi
# check config file exist
if [ ! -f "$config_file" ]
then
echo "config file: $config_file not found"
exit 1
fi
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
status)
do_status
;;
*)
usage
;;
esac
exit 0