Centos开机自启动redis

来源:互联网 发布:斗鱼程序员 工资待遇 编辑:程序博客网 时间:2024/06/11 07:49

原文链接:http://my.oschina.net/indestiny/blog/197272?p={{page}}

  • 修改redis.conf,打开后台运行选项:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # By default Redis does not run as a daemon. Use 'yes' if you need it.  
  2. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.  
  3. daemonize yes  

  • 编写脚本,vim /etc/init.d/redis:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # chkconfig: 2345 10 90  
  2. # description: Start and Stop redis  
  3.    
  4. PATH=/usr/local/bin:/sbin:/usr/bin:/bin  
  5.    
  6. REDISPORT=6379 #实际环境而定  
  7. EXEC=/usr/local/redis/src/redis-server #实际环境而定  
  8. REDIS_CLI=/usr/local/redis/src/redis-cli #实际环境而定  
  9.    
  10. PIDFILE=/var/run/redis.pid  
  11. CONF="/usr/local/redis/redis.conf" #实际环境而定  
  12.    
  13. case "$1" in  
  14.         start)  
  15.                 if [ -f $PIDFILE ]  
  16.                 then  
  17.                         echo "$PIDFILE exists, process is already running or crashed."  
  18.                 else  
  19.                         echo "Starting Redis server..."  
  20.                         $EXEC $CONF  
  21.                 fi  
  22.                 if [ "$?"="0" ]  
  23.                 then  
  24.                         echo "Redis is running..."  
  25.                 fi  
  26.                 ;;  
  27.         stop)  
  28.                 if [ ! -f $PIDFILE ]  
  29.                 then  
  30.                         echo "$PIDFILE exists, process is not running."  
  31.                 else  
  32.                         PID=$(cat $PIDFILE)  
  33.                         echo "Stopping..."  
  34.                         $REDIS_CLI -p $REDISPORT SHUTDOWN  
  35.                         while [ -x $PIDFILE ]  
  36.                         do  
  37.                                 echo "Waiting for Redis to shutdown..."  
  38.                                 sleep 1  
  39.                         done  
  40.                         echo "Redis stopped"  
  41.                 fi  
  42.                 ;;  
  43.         restart|force-reload)  
  44.                 ${0} stop  
  45.                 ${0} start  
  46.                 ;;  
  47.         *)  
  48.                 echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
  49.                 exit 1  
  50. esac  

  • 执行权限:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. chmod +x /etc/init.d/redis  
  • 开机自启动:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # 尝试启动或停止redis  
  2. service redis start  
  3. service redis stop  
  4.    
  5. # 开启服务自启动  
  6. chkconfig redis on  
0 0
原创粉丝点击