1.为了让redis-server能在系统启动时自动运行,需要将redis服务作为守护进程(daemon)来运行,我们回/usr/local/cluster/7000/目录中找到一个redis.conf的文件,这个文件是redis服务运行时加载的配置,我们先观察一下其中的内容[[email protected]192 7000]# vi redis.conf输入/daemonize
此文件内容非常长,但是大部分是注释,我们重点关注其中的几个设置daemonize和pidfile:其中daemonize默认值是false,pidfile默认值是pidfile /var/run/redis_6379.pid第一个表示是否daemon化,显然我们要把它改成daemonize yes;第二个表示当服务以守护进程方式运行时,redis默认会把pid写入/var/run/redis_6379.pid文件,服务运行中该文件就存在,服务一旦停止该文件就自动删除,因而可以用来判断redis是否正在运行。 保存后退出。有了基本配置,redis还需要有一个管理启动、关闭、重启的一个脚本。redis源码里其实已经提供了一个初始化脚本,位值在/usr/redis/utils/redis_init_script。我们来看看这个脚本做了些什么:
#!/bin/sh# redisport=6379 exec=/usr/Local/bin/redis-server cliexec=/usr/LOCAL/BIN/REDIS-CLI Pidfile=/var/run/redis_${redisport}.pid conf="/etc/redis/${redisport}.conf "Case"$ "In start)if [-F$PIDFILE]ThenEcho"$PIDFILE exists, process is already running or crashed "ElseEcho"Starting Redis server ..."$EXEC$CONFfi;; Stopif [!-F$PIDFILE]ThenEcho"$PIDFILE does not exist, process was not running "Else pid=$ (cat$PIDFILE)echo "stopping ..." $CLIEXEC-P $REDISPORT shutdown while [-x/proc/${pid}] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" FI;; *) echo "Please use Start or stop as first argument";; esac script specifies the port, server path, CLI path, Pidfile path, and conf path, the above yellow places need to be properly configured, say more, if you perform the make during installation Install, then the script here does not need to do much change, because make install the server and CLI are copied to/usr/local/bin below. My script
***
2. Set the boot file configuration and go to the 7000 root directory
cd/usr/local/cluster/7000
Mkdir/etc/redis
CP redis.conf/etc/redis/7000.conf
***
3. Copy the startup script to the/ETC/INIT.D directory, this example name the startup script Redis (copy/usr/local/cluster/7000/utils/redis_init_script file)
CP Redis_init_script/etc/init.d/redis
In accordance with the 1th said to modify the Redis file, note the increase of chkconfig and description two lines will not prompt the service REDISD does not support Chkconfig?
***
#!/bin/sh
#
# simple Redis INIT.D script conceived to work on Linux systems
# as it does use of the/proc filesystem.
# chkconfig:2345 90 10
# Description:redis is a persistent key-value database
redisport=7000
Exec=/usr/local/cluster/7000/src/redis-server
Cliexec=/usr/local/cluster/7000/src/redis-cli
Pidfile=/var/run/redis_${redisport}.pid
conf= "/etc/redis/${redisport}.conf"
Case "$" in
Start
If [-F $PIDFILE]
Then
echo "$PIDFILE exists, process is already running or crashed"
Else
echo "Starting Redis server ..."
$EXEC $CONF
Fi
;;
Stop
After editing the Save is OK
***
4. #设置为开机自启动服务器
Chkconfig Redis on
#打开服务
Service Redis Start
#关闭服务
Service Redis Stop
***
5. Restart reboot
Perform the PS command to see that the Redis 7000 port has been started
Linux centOS7 setup Redis boot up