Configure the Redis boot startup script under Centos
- 2020-05-17 07:32:28
- OfStack
1. Download and install
wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz
tar -zxf redis-2.2.13.tar.gz
cd redis-2.2.13
make
sudo make install
cp redis.conf /etc
At install, the redis command is copied under /usr/local/bin
2. Set up user and log directory
Before starting Redis for the first time, it is recommended to create a separate user for Redis and create a new data and log folder
sudo useradd redis
sudo mkdir -p /var/lib/redis
sudo mkdir -p /var/log/redis
sudo chown redis.redis /var/lib/redis # db The file is here to be modified redis.conf
sudo chown redis.redis /var/log/redis
Configure the init script
Actually, there are a lot of startup scripts written by foreigners on github, but most of them are written by ubuntu, for Centos.
It has been modified as follows:
###########################
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
##############################
Save the above code as redis and put it under /etc/ init.d /
chmod +x /etc/init.d/redis
If you want it to run as redis-server in the background, then
redis.conf needs to be modified to change daemonize no to daemonize yes
4. Set the startup service
sudo chkconfig redis on
5. Start and stop redis
service redis start # or /etc/init.d/redis start
service redis stop # or /etc/init.d/redis stop
6. Test redis
redis-cli
redis 127.0.0.1:6379> set foo 123
OK
redis 127.0.0.1:6379> get foo
"123"
redis 127.0.0.1:6379> exit