Centos下配置Redis开机启动脚本
程序员文章站
2023-02-25 11:29:14
1、下载安装
wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz
tar -zxf re...
1、下载安装
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
install的时候,redis的命令会被拷贝到/usr/local/bin下面
2,建立用户与日志目录
第一次启动redis前,建议为redis单独建立一个用户,并新建data和日志文件夹
sudo useradd redis sudo mkdir -p /var/lib/redis sudo mkdir -p /var/log/redis sudo chown redis.redis /var/lib/redis #db文件放在这里,要修改redis.conf sudo chown redis.redis /var/log/redis
3,配置init脚本
其实github上很多老外写好的启动脚本,不过大部分都是ubuntu的,对于centos。
经过修改,如下:
########################### 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 ##############################
把上述代码存为redis,放到/etc/init.d/下面
chmod +x /etc/init.d/redis
其实做成服务启动,也是调用redis-server,如果想让它在后台作为daemon运行,那么
需要修改redis.conf,将 daemonize no 改为 daemonize yes
4,设定开机启动服务
sudo chkconfig redis on
5,启动,停止redis
service redis start #或者 /etc/init.d/redis start service redis stop #或者 /etc/init.d/redis stop
6,测试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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。