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

#!/bin/bash
#*****************************************************
#         Author: suixiaofeng
#           blog:https://blog.cool360.org
#          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]#

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值