linux之redis启动脚本编写v1.0

3年前 (2017-06-30) gtj linux, shell 0评论 已收录 189℃

编写一个redis的启动关闭脚本
脚本内容如下:


#!/bin/bash
#*****************************************************
#         Author: suixiaofeng
#           blog:https://bk.devopstack.cn
#          Email: 258818040@qq.com 
#  Last modified: 2017-06-30 19:34
#       Filename: redis
#    Description: 
#****************************************************
[ -f /etc/init.d/functions ] && . /etc/init.d/functions

redis_server="/u02/redis/bin/redis-server"
redis_conf="/u02/redis/conf/redis.conf"

[ -x /u02/redis/bin/redis-server -a  -f /u02/redis/conf/redis.conf  ] || {
    echo "redis is not install."
    exit 1
}

if [ $# -ne 1 ] 
 then  
  echo $"usage:$0 {start|stop|restart|status}"
  exit 1
fi

function start () {
  stapro=`netstat -lntup|grep redis |wc -l` 
     if [ $stapro -ne 0 ] ; then 
        echo -e "\033[32mredis  is running \033[0m"  
        exit 2
     else  
         ${redis_server}   ${redis_conf}
         sleep 2
         stapro1=`netstat -lntup|grep redis |wc -l` 
         [ $stapro1 -ne 0 ] &&{ 
         action  "redis is started"  /bin/true
         exit 0
        }
     fi
}

function stop () {
  stopro=`netstat -lntup|grep redis |wc -l` 
     if [ $stopro -eq 0 ] ; then
        echo -e "\033[32mredis  is stopped \033[0m"  
        exit 2
     else
      #   PID=`ps aux|grep redis|grep -v grep|awk '{print $2}'`
      #    kill -9 ${PID} >/dev/null
            killproc redis-server
          sleep 2
         stopro1=`netstat -lntup|grep redis |wc -l`
         [ $stopro1 -eq 0 ] &&{
         action  "redis is stopped"  /bin/true
         exit 0
        }
     fi
}

function  status () {

  statpro=`netstat -lntup|grep redis |wc -l` 
  if [ $statpro -eq 0 ] ; then
      echo -e "\033[32mredis  is stopped \033[0m "   
  else
     echo -e "\033[32mredis  is running \033[0m "  
  fi 
 exit 0
}

case  $1 in
   "start") 
        start
         ;;
   "stop")
      stop
       ;;
   "restart")
         stop
         start
         ;;
   "status")
       status
       ;;

    * )
     echo $"Usage:$0 {start|stop|restart|status}"
      exit 4
esac
exit 0

接着把脚本放在/etc/init.d下,赋权 chmod +x /etc/init.d/redis

操作如下:

[root@sf106232 srv]# /etc/init.d/redis status
redis  is stopped  
[root@sf106232 srv]# /etc/init.d/redis 
usage:/etc/init.d/redis {start|stop|restart|status}
[root@sf106232 srv]# /etc/init.d/redis start
redis is started                                           [  OK  ]
[root@sf106232 srv]# /etc/init.d/redis status
redis  is running  
[root@sf106232 srv]# /etc/init.d/redis stop
redis is stopped                                           [  OK  ]
[root@sf106232 srv]# /etc/init.d/redis status
redis  is stopped  
[root@sf106232 srv]# 
[root@sf106232 u02]# service redis status
redis  is stopped  
[root@sf106232 u02]# service redis start
redis is started                                           [  OK  ]
[root@sf106232 u02]# service redis stop
redis is stopped                                           [  OK  ]
[root@sf106232 u02]# service redis status
redis  is stopped  
加入开机自启失败。
[root@sf106232 u02]# chkconfig --add redis
service redis does not support chkconfig
在脚本里加入:
# chkconfig: - 20 80   #开机启动和关闭的优先级
# description: Starts and stops the redis daemon.
然后就可以加入开机自启了
[root@sf106232 u02]# chkconfig --add redis
[root@sf106232 u02]# chkconfig --list|grep redis
redis          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@sf106232 u02]# 
[root@sf106232 u02]# chkconfig  redis on
[root@sf106232 u02]# chkconfig --list|grep redis
redis          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@sf106232 u02]# 

这个版本为最初的,可以参考标准的去完善。

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!