-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.sh
executable file
·102 lines (96 loc) · 2.82 KB
/
redis.sh
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
#!/bin/bash
NAME=redis
FILE=redis.pid
# 判断redis目录是否存在, 如果不存在则创建
is_directory()
{
if [ ! -d $1 ]; then
echo "$1 目录创建中..."
mkdir $1
if [ $? -ne 0 ];then
echo "$1 目录创建失败, ~~~~(>_<)~~~~"
exit 1
fi
fi
}
# 判断redis目录是否存在, 如果不存在则创建
is_regfile()
{
if [ ! -f $1 ]; then
#statements
echo "$1 file not exist..."
return 1
fi
return 0
}
# 根据参数设置redis状态
if [[ $1 = "" ]];then
echo "please input argument:"
echo " start: start redis server"
echo " stop: stop redis server"
echo " status: show the redis server status"
exit 1
fi
# 函数调用
is_directory $NAME
case $1 in
start)
# 判断 redis-server 进程是否已经启动...
ps aux | grep "redis-server" | grep -v grep > /dev/null
if [ $? -eq 0 ];then
echo "Redis server is runing ..."
else
# 删除$FILE 文件
unlink "$NAME/$FILE"
echo "Redis starting ..."
redis-server ./conf/redis.conf
if [ $? -eq 0 ];then
echo "Redis server start success!!!"
# 休眠1s, 等待pid文件被创建出来, 再进行后续判断
sleep 1
if is_regfile "$NAME/$FILE";then
printf "****** Redis server PID: [ %s ] ******\n" $(cat "$NAME/$FILE")
printf "****** Redis server PORT: [ %s ] ******\n"
$(awk '/^port /{print $2}' "./conf/redis.conf")
fi
fi
fi
;;
stop)
# 判断 redis-server 进程是否已经启动...
ps aux | grep "redis-server" | grep -v grep > /dev/null
if [ $? -ne 0 ];then
echo "Redis server is not runing..."
exit 1
fi
echo "Redis stopping ..."
# 判断pid文件是否存在
if is_regfile "$NAME/$FILE"; then
# 读进程文件
echo "### 通过 redis.pid文件 方式关闭进程 ###"
PID=$(cat "$NAME/$FILE")
else
# 查找进程ID
echo "### 通过 查找进程ID 方式关闭进程 ###"
PID=$(ps aux | grep "redis-server" | grep -v grep | awk '{print $2}')
fi
echo Redis server pid = $PID
kill -9 $PID
if [ $? -ne 0 ]; then
echo "Redis server stop fail ..."
else
echo "Redis server stop success!!!"
fi
;;
status)
ps aux | grep "redis-server" | grep -v grep > /dev/null
if [ $? -eq 0 ];then
echo "Redis server is running..."
else
echo "Redis server is not running ..."
fi
;;
*)
echo "do nothing ..."
;;
esac