安装配置完 redis之后 启动redis-server 小小兴奋了一把 哥也高大上了 但是启动了redis-server之后就只能换个窗口操作redis-cli 感觉很不爽的样子 网上查了下资料 把redis启动与关闭 做成后台进程。接下来与大家分享一下。
1、copy 命令到/usr/local/bin目录下
cp redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/bin
2、copy redis.conf 到/etc 目录下
cp redis.conf /etc 并将文件中将 daemonize no 改为 daemonize yes 表示能在后台运行
3、配置环境变量
echo "PATH=$PATH:/usr/local/bin" >> /etc/profile
重新加载 source /etc/profile
4、新建redis脚本 放入/etc/init.d/文件夹下 下面就是脚本 然后加上操作权限 chmod +x redis
# description: Redis is a persistent key-value database
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.pid
CONF="/etc/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
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
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
esac
5、设置开机启动
sudo chkconfig redis on
6、启动或停止redis服务
service redis start service redis stop
好了 世界又和平了 我们可以happy的用redis了