1 #!/bin/sh 2 # 3 # chkconfig: - 39 35 4 # description: start and stops redis server 5 # programname: redis 6 7 # Source function library. 8 if [ -f /etc/init.d/functions ] ; then 9 . /etc/init.d/functions 10 elif [ -f /etc/rc.d/init.d/functions ] ; then 11 . /etc/rc.d/init.d/functions 12 else 13 exit 1 14 fi 15 16 gProPath="/opt/redis" 17 gConfPath="/etc/redis" 18 gDataPath="/var/redis" 19 gProName="redis-server" 20 gName="redis" 21 gPidfile="${gDataPath}/redis.pid" 22 gDaemon="${gProPath}/bin/${gProName}" 23 gCli="${gProPath}/bin/redis-cli" 24 gSocket="${gDataPath}/redis.sock" 25 gConfig="${gConfPath}/redis.conf" 26 gLogPath="${gDataPath}/redis/" 27 gUser="redis" 28 gRetVal=0 29 30 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 31 set -e 32 33 [[ -z $(grep "^${gUser}:" /etc/passwd) ]] && useradd ${gUser} 34 gGroup=$(groups ${gUser} | gawk '{print $3}') 35 chown -R ${gUser}:${gGroup} ${gProPath} 36 chown -R ${gUser}:${gGroup} ${gConfPath} 37 38 start() 39 { 40 [[ -e ${gDataPath} ]] || 41 { 42 mkdir -p ${gDataPath} 43 chown -R ${gUser}:${gGroup} ${gDataPath} 44 chmod -R 700 ${gDataPath} 45 } 46 if [ -e $gPidfile ];then 47 echo -n "redis runing" 48 exit 1 49 fi 50 [[ -d ${gLogPath} ]] || 51 { 52 mkdir -p ${gLogPath} 53 chown -R ${gUser}:${gGroup} ${gLogPath} 54 chmod -R 700 ${gLogPath} 55 } 56 echo -n "start ${gProName}" 57 su - ${gUser} -c " ${gDaemon} ${gConfig}" #切换用户执行命令。 这里以redis用户启动redis 58 echo 59 return $? 60 } 61 62 stop() 63 { 64 if [ ! -f $gPidfile ];then 65 echo -n "redis-master is not runing" 66 exit 1 67 fi 68 echo -n "stop ${gProName}" 69 lpid=`cat ${gPidfile}` 70 kill -9 $lpid 71 #su - ${gUser} -c " ${gCli} -s ${gSocket} -a ${gPasswd} SHUTDOWN " 72 gRetVal=$? 73 [[ ${gRetVal} -eq 0 ]] && 74 { 75 echo_success 76 rm -rf ${gSocket} 77 rm -rf ${gPidfile} 78 } || echo_failure 79 echo 80 return ${gRetVal} 81 } 82 83 restart() 84 { 85 stop 86 sleep 1 87 start 88 } 89 90 rhstatus () 91 { 92 su - ${gUser} -c "status ${gProName}" 93 } 94 95 case "$1" in 96 start) 97 start 98 ;; 99 stop) 100 stop 101 ;; 102 restart) 103 restart 104 ;; 105 status) 106 rhstatus 107 ;; 108 *) 109 echo $"Usage: $0 {start|stop|restart|status}" 110 exit 1 111 esac 112 113 exit 0