频道栏目
首页 > 数据库 > Sybase > 正文
redis基础4-redis服务
2015-06-23 11:55:54         来源:思考在编码之前解决问题  
收藏   我要投稿

redis配置文件

主要遇到的问题是:redis.pid没有找到

下面是我的配置文件

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.

var/run的执行权限

lrwxrwxrwx  1 root root        4 Feb 24 06:57 run -> /run

然后我用sudo touch redis.pid


root@ubuntu:/home/fuhui/redis-2.8.19/utils# pwd
/home/fuhui/redis-2.8.19/utils

root@ubuntu:/home/fuhui/redis-2.8.19/utils# sh redis_init_script stop
/var/run/redis_6379.pid does not exist, process is not running

不知道是不是权限的问题,修改o权限

fuhui@ubuntu:/var/run$ sudo chmod o+wx redis.pid
-rw-r--rwx 1 root       root          0 Jun 22 08:39 redis.pid

难道是服务并没有重启?

fuhui@ubuntu:/var/run$ ps -ef | grep 'redis'
fuhui     2452     1  0 07:55 ?        00:00:04 redis-2.8.19/src/redis-server *:6379                 
fuhui     2908  2756  0 08:46 pts/13   00:00:00 grep --color=auto redis
fuhui@ubuntu:/var/run$ kill -9 2452
fuhui@ubuntu:/var/run$ ps -ef | grep 'redis'
fuhui     2910  2756  0 08:46 pts/13   00:00:00 grep --color=auto redis

重启一下试试:
搞定,redis.pid里有了pid号


关闭redis服务

root@ubuntu:/home/fuhui/redis-2.8.19/utils# sh redis_init_script stop
/var/run/redis_6379.pid does not exist, process is not running

修改shell文件
下面的是原文件,我们要做的其实很简单,只是把路径修改成实际存放的就可以

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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

修改如下:

root@ubuntu:/home/fuhui/redis-2.8.19/utils# cp redis_init_script ../redis_script

看一下proc下是否存在pid

fuhui@ubuntu:/proc$ cd /proc/
fuhui@ubuntu:/proc$ ls -lh | grep 2912
dr-xr-xr-x  9 root       root          0 Jun 22 08:47 2912

关闭redis服务测试


fuhui@ubuntu:~/redis-2.8.19$ src/redis-cli
127.0.0.1:6379> shutdown
not connected> 

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

#REDISPORT=6379
EXEC=/home/fuhui/redis-2.8.19/src/redis-server
CLIEXEC=/home/fuhui/redis-2.8.19/src/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/home/fuhui/redis-2.8.19/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  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

执行结果

root@ubuntu:/home/fuhui/redis-2.8.19# sh redis_script  start
Starting Redis server...
root@ubuntu:/home/fuhui/redis-2.8.19# ps -ef | grep 'redis'
root      2972     1  0 09:08 ?        00:00:00 /home/fuhui/redis-2.8.19/src/redis-server *:6379                             
root      2993  2720  0 09:09 pts/6    00:00:00 grep --color=auto redis
root@ubuntu:/home/fuhui/redis-2.8.19# sh redis_script  stop
Stopping ...
Redis stopped

root@ubuntu:/home/fuhui/redis-2.8.19# ps -ef | grep 'redis'
root      2998  2720  0 09:10 pts/6    00:00:00 grep --color=auto redis
点击复制链接 与好友分享!回本站首页
相关TAG标签 基础
上一篇:一起talkGCC吧(第二回:GCC使用)
下一篇:内网映射到外网的工具ngrok使用
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站