在redis源码目录下有一个utils文件夹,提供了install_server.sh安装工具,可生成自启动的redis脚本。
[[email protected]]# cd utils/ [[email protected] 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: [6379] Selectingdefault: 6379 Pleaseselect the redis config file name [/etc/redis/6379.conf] /usr/local/redis/etc/redis.conf Pleaseselect the redis log file name [/var/log/redis_6379.log] /usr/local/redis/log/redis.log Pleaseselect the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/data/ Pleaseselect the redis executable path [/usr/local/redis/bin/redis-server] 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数据存储目录
脚本内容如下:
[[email protected] utils]# 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
添加服务并更改运行级别:
[[email protected] utils]# chkconfig --add redi_6379 [[email protected] utils]# chkconfig --level redi_6379 35 on
时间: 2024-10-20 02:44:50