jyhrte 发表于 2015-4-15 11:07:58

配置redis自启动脚本

在redis源码目录下有一个utils文件夹,提供了install_server.sh安装工具,可生成自启动的redis脚本。         

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@huntdbredis-2.8.19]# cd utils/
# ./install_server.sh
Welcometo the redis service installer
Thisscript will help you easily set up a running redis server

Pleaseselect the redis port for this instance:
Selectingdefault: 6379
Pleaseselect the redis config file name /usr/local/redis/etc/redis.conf
Pleaseselect the redis log file name /usr/local/redis/log/redis.log
Pleaseselect the data directory for this instance /usr/local/redis/data/
Pleaseselect the redis executable path
Selectedconfig:
Port         : 6379
Configfile    : /usr/local/redis/etc/redis.conf
Logfile       :/usr/local/redis/log/redis.log
Datadir       : /usr/local/redis/data/
Executable   : /usr/local/redis/bin/redis-server
CliExecutable : /usr/local/redis/bin/redis-cli
Is thisok? Then press ENTER to go on or Ctrl-C to abort.
Copied/tmp/6379.conf => /etc/init.d/redis_6379
Installingservice...
Successfullyadded to chkconfig!
Successfullyadded to runlevels 345!
StartingRedis server...
Installationsuccessful!




根据实际情况分别填写配置文件、日志文件、rdb/aof数据存储目录

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# cat /etc/init.d/redis_6379
#!/bin/sh
#Configurationsinjected by install_server below....

EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/etc/redis.conf"
REDISPORT="6379"
###############
# SysVInit Information
#chkconfig: - 58 74
#description: redis_6379 is the redis daemon.
###BEGIN INIT INFO
#Provides: redis_6379
#Required-Start: $network $local_fs $remote_fs
#Required-Stop: $network $local_fs $remote_fs
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Should-Start: $syslog $named
#Should-Stop: $syslog $named
#Short-Description: start and stop redis_6379
#Description: Redis daemon
### ENDINIT INFO


case"$1" in
    start)
      if [ -f $PIDFILE ]
      then
            echo "$PIDFILE exists, processis already running or crashed"
      else
            echo "Starting Redisserver..."
            $EXEC $CONF
      fi
      ;;
    stop)
      if [ ! -f $PIDFILE ]
      then
            echo "$PIDFILE does not exist,process is not running"
      else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis toshutdown ..."
               sleep 1
            done
            echo "Redis stopped"
      fi
      ;;
    status)
      PID=$(cat $PIDFILE)
      if [ ! -x /proc/${PID} ]
      then
            echo 'Redis is not running'
      else
            echo "Redis is running($PID)"
      fi
      ;;
    restart)
      $0 stop
      $0 start
      ;;
    *)
      echo "Please use start, stop,restart or status as first argument"
      ;;
esac




添加服务并更改运行级别:

1
2
3
# chkconfig --add redi_6379

# chkconfig --level redi_6379 35 on



页: [1]
查看完整版本: 配置redis自启动脚本