shell 編寫redis啓動腳本

安裝後redis,默認系統不會自啓動,若是關機或重啓redis不會自行啓動,linux下/etc/init.d/目錄下基本上存放全部系統的大多數的啓動腳本,放在這個目錄下的腳本能夠實現自啓動操做。linux

 

在 /etc/init.d/目錄下建立redis的shell文件redis

複製代碼
#!/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
複製代碼

 

都正常shell

 

將redis加入自啓動計劃bash

cd /etc/init.d/spa

chmod -R 775 redisrest

chkconfig --list|grep rediscode

添加啓動計劃到系統server

[root@dongzi init.d]# chkconfig --add redis
service redis does not support chkconfi

再在腳本里面加入兩行註釋blog

# chkconfig: - 20 80
# description: Starts and stops the redis daemon.ip

運行正常

複製代碼
[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
相關文章
相關標籤/搜索