#! /bin/bash # chkconfig: 2345 10 90 #第一个数字是服务的运行级,2345表明这个服务的运行级是2、3、4和5级(Linux的运行级为0到6);第二个数字是启动优先级,数值从0到99;第三个数是停止优先级,数值也是从0到99; # description: Start and Stop redis 对服务的描述。 PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/usr/local/redis/redis.conf" AUTH="Redis@123" HOST="10.102.230.7" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed." else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE exists, process is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -h $HOST -p $REDISPORT -a $AUTH SHUTDOWN sleep 2 while [ -x $PIDFILE ] do echo "Waiting for Redis to shutdown..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1
将上述脚本拷贝到/etc/init.d/下面
添加脚本的执行权限
chmod 777 redis-auto.sh 我的脚本名字叫redis-auto.sh
将脚本添加到系统服务
chkconfig --add redis-auto.sh
查看服务是否添加成功
chkconfig -- list
然后就可以使用systemctl start|status|stop|restart redis来控制redis的启动啦。