网上有很多redis在linux下自动启动的例子,实现的方式很多,很多都是参考一个老外流传出来启动的例子,其实直接使用是不行,而且有很多地方有一些语法错误,这里就讲我实验过,成功的Linux服务chkconfig配置启动的方法。
编写脚本,vim /etc/init.d/redis:
# chkconfig: 2345 90 10 # description: Redis is a persistent key-value database PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 #实际环境而定 EXEC=/opt/redis-3.0.4/src/redis-server #实际环境而定 REDIS_CLI=/opt/redis-3.0.4/src/redis-cli #实际环境而定 PIDFILE=/var/run/redis.pid CONF="/opt/redis-3.0.4/redis.conf" #实际环境而定 #AUTH="1234" 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 -p $REDISPORT SHUTDOWN #$REDIS_CLI -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)# chkconfig: 2345 90 10 # description: Redis is a persistent key-value database PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 #实际环境而定 EXEC=/opt/redis-3.0.4/src/redis-server #实际环境而定 REDIS_CLI=/opt/redis-3.0.4/src/redis-cli #实际环境而定 PIDFILE=/var/run/redis.pid CONF="/opt/redis-3.0.4/redis.conf" #实际环境而定 #AUTH="1234" 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 -p $REDISPORT SHUTDOWN #$REDIS_CLI -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 esacstop# chkconfig: 2345 90 10 # description: Redis is a persistent key-value database PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 #实际环境而定 EXEC=/opt/redis-3.0.4/src/redis-server #实际环境而定 REDIS_CLI=/opt/redis-3.0.4/src/redis-cli #实际环境而定 PIDFILE=/var/run/redis.pid CONF="/opt/redis-3.0.4/redis.conf" #实际环境而定 #AUTH="1234" 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 -p $REDISPORT SHUTDOWN #$REDIS_CLI -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 esacstart ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
# chkconfig: 2345 10 90
表示通过chkconfig配置,在linux中2、3、4、5项中启动,这个2345代表就linux系统的启动顺序具体,具体内容是:等级0表示:表示关机、等级1表示:单用户模式、等级2表示:无网络连接的多用户命令行模式、等级3表示:有网络连接的多用户命令行模式、等级4表示:不可用、 等级5表示:带图形界面的多用户模式、 等级6表示:重新启动
如果看不明白,自己百度 chkconfig命令详细说明。
AUTH=”1234″
如果redis设置了登录密码,就需要这个配置。
如果找不到pid文件,需要配置redis.conf配置中的选项打开。
修改redis.conf,打开后台运行选项(很多人都会有这种情况,我的也是):
daemonize yes
添加执行权限:
chmod 755 /etc/init.d/redis
开机自启动:
chkconfig redis on
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/98625.html