安装后redis,默认系统不会自启动,如果关机或重启redis不会自行启动,linux下/etc/init.d/目录下基本上存放所有系统的大多数的启动脚本,放在这个目录下的脚本可以实现自启动操作。
在 /etc/init.d/目录下创建redis的shell文件
#!/bin/bash
#config:/usr/local/src/redis.conf
#pidfile:/var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/bin"
REDIS_CONFIG="/usr/local/src/redis.conf"
PIDFILE="/var/run/redis.pid"
###Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=
prog="redis-server"
desc="Redis Server" start(){ if [ -e $PIDFILE ];then
echo "$desc already running...."
exit
fi echo -n $"starting $desc:" daemon $BIN/$prog $REDIS_CONFIG & RETVAL=$? echo [ $RETVAL -eq ] && touch /var/lock/subsys/$prog
return $RETVAL } stop(){
echo -n $"Stop $desc" killproc $prog
RETVAL=$?
echo [ $RETVAL -eq ] && rm -f /var/lock/subsys/$prog $PIDFILE return $RETVAL } restart(){
stop
start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status $prog RETVAL=$? ;; *) echo $"Usage:$0{start|stop|restart|condrestart|status}" RETVAL= esac
exit $RETVAL
service redis start service redis stop service redis restart service redis status
都正常
将redis加入自启动计划
cd /etc/init.d/
chmod -R 775 redis
chkconfig --list|grep redis
添加启动计划到系统
[root@dongzi init.d]# chkconfig --add redis
service redis does not support chkconfi
再在脚本里面加入两行注释
# chkconfig: - 20 80
# description: Starts and stops the redis daemon.
运行正常
[root@dongzi init.d]# chkconfig --add redis
You have new mail in /var/spool/mail/root
[root@dongzi init.d]# chkconfig --list|grep redis
redis :off :off :off :off :off :off :off
You have new mail in /var/spool/mail/root
[root@dongzi init.d]# chkconfig --level redis on
[root@dongzi init.d]# chkconfig --list|grep redis
redis :off :off :on :on :on :on :off