shell编写redis启动脚本

安装后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=0
prog="redis-server"
desc="Redis Server"
 

start(){

    if [ -e $PIDFILE ];then
        echo "$desc already running...."
        exit 1
    fi

    echo -n $"starting $desc:"

    daemon  $BIN/$prog $REDIS_CONFIG &

    RETVAL=$?

    echo 

    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL

}
 

stop(){
    echo -n $"Stop $desc"

    killproc $prog
    RETVAL=$?
    echo

    [ $RETVAL -eq 0 ] && 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=1

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              0:off    1:off    2:off    3:off    4:off    5:off    6:off
You have new mail in /var/spool/mail/root
[root@dongzi init.d]# chkconfig --level 2345 redis on
[root@dongzi init.d]# chkconfig --list|grep redis
redis              0:off    1:off    2:on    3:on    4:on    5:on    6:off

 

 

posted @ 2016-11-08 16:20  李思琼  阅读(4489)  评论(2编辑  收藏  举报