1 #!/bin/bash 2 PORT=6379
3 NAME=redis-server 4 ID=`ps -ef | grep "$NAME" | grep -v "grep" | awk '{print $2}'` 5 #CHECK_PORT=`netstat -tnlp|grep "\b$PORT\b"` 6 REDIS_SERVER=/usr/local/redis/bin/redis-server 7 REDIS_CONFIG=/usr/local/redis/etc/redis.conf 8 RETAVL=0
9 #检查shelk公共函数库是否存在,存在就加载 10 FUNCTIONS_PATH=/etc/init.d/functions 11 [ -f $FUNCTIONS_PATH ]&& source $FUNCTIONS_PATH 12 #检查redis文件是否存在并可执行 13 [ -x $REDIS_SERVER ]|| exit 0
14
15 #定义函数 16 #检查是否执行成功 17 check(){ 18 RETAVL=$?
19 if
20 [ $RETAVL -eq 0 ];then
21 action "redis is $1" /bin/true
22 else
23 action "redis is $1" /bin/false
24 fi
25 } 26 #启动服务 27 start(){ 28 $REDIS_SERVER $REDIS_CONFIG 29 RETVAL=$?
30 if [ $RETVAL -eq 0 ]; then
31 echo "redis is started!"; 32 else
33 echo "redis start failed!"; 34 fi
35 return $RETAVL 36
37 } 38 #停止服务 39 stop(){ 40 for id in $ID 41 do
42 kill -9 $id
43 done
44 RETVAL=$?
45 if [ $RETVAL -eq 0 ]; then
46 echo "redis is stopped!"; 47 else
48 echo "redis stop failed!"; 49 fi
50 return $RETVAL; 51 } 52
53 #redis启动状态 54 status(){ 55 STATUS=$(pgrep redis-server | wc -l) 56 if [[ $STATUS -eq 0 ]];then
57 echo "redis is not running!"; 58 else
59 echo "redis is running!"; 60 fi
61 } 62
63 #重启服务 64 restart(){ 65 stop 66 sleep 1
67 start 68 } 69
70 #判断 71 case "$1" in
72 start) 73 start 74 ;; 75 stop) 76 stop 77 ;; 78 status) 79 status 80 ;; 81 restart) 82 restart 83 ;; 84 *) 85 echo $"Usage:$0{start|stop|restart|help}"
86 esac
87 exit $RETAVL