redis 脚本位于 redis 安装目录
redis/utils/redis_init_script
文件
创建 脚本 文件
vi /etc/init.d/redis-6379
编写脚本文件
#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run
# 端口需要与配置文件 一致
REDISPORT=6379
#注意自己安装的redis根目录
REDISPATH=/usr/local/redis/redis-6379
EXEC=${REDISPATH}/src/redis-server
CLIEXEC=${REDISPATH}/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="${REDISPATH}/redis.conf"
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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is 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
设置权限,让Linux 可执行
chmod 755 /etc/init.d/redis-6379
服务测试
# 启动
services redis-6379 start
# 停止
services redis-6379 stop
设置开机启动
chkconfig redis-6379 on