870   Redis Ubuntu

一,添加系统启动方法:service redis-server {start|stop|restart|force-reload|status}

 

1,在/etc/init.d/目录下,新增文件名为redis-server,内容如下:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          redis-server
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Enable service provided by redis-server daemon.
### END INIT INFO

NAME="redis-server"
PATH="/usr/local/bin:/sbin:/usr/bin:/bin"
# redis-server 启动脚本和配置文件
SERVICE="/usr/share/redis/src/redis-server"  
CONF="/usr/share/redis/redis.conf"  

exist_service()
{
	EXIST=`ps -ef | grep ${SERVICE} | grep -v grep | awk '{print $2}' | xargs`
	if [ -z ${EXIST} ]
	then 
		echo "${NAME} is not running"
		return 0
	else
		echo "${NAME} is running"
		return 1
	fi
}

start_service()
{
    exist_service
    if [ $? -eq 0 ]
    then   
 	echo "Starting ..."
		${SERVICE} ${CONF}
		sleep 1
		exist_service
		if [ $? -eq 1 ]
		then
			echo "${NAME} start successfully"
		else
			echo "failed to start ${NAME}"
		fi
    fi
}

stop_service()
{
    exist_service
    if [ $? -eq 1 ]
    then
	echo "stopping ..."
		ps -ef | grep ${SERVICE} | grep -v grep | awk '{print $2}' | xargs kill -9
		sleep 1
		exist_service
		if [ $? -eq 0 ]
		then
			echo "${NAME} stop successfully"
		else
			echo "failed to stop ${NAME}"
		fi
    fi
	
}



case "$1" in  
	start)
		start_service
		;;
	stop)
		stop_service
		;;
	restart|force-reload)
                stop_service
		start_service
		;;
	status)
		exist_service
    ;;
  *)
    echo "Usage: service ${NAME} {start|stop|restart|force-reload|status}" >&2
    exit 1
esac

 

2,测试启动脚本:

 

$ sudo service redis-server start
redis-server is not running
Starting ...
redis-server is running
redis-server start successfully
$ sudo service redis-server status
redis-server is running
$ sudo service redis-server stop
redis-server is running
stopping ...
redis-server is not running
redis-server stop successfully

 

二,将脚本添加到开机启动项目中

 

1,添加到开机启动项,重启Ubuntu测试下吧

$ sudo update-rc.d redis-server defaults
 Adding system startup for /etc/init.d/redis-server ...
   /etc/rc0.d/K20redis-server -> ../init.d/redis-server
   /etc/rc1.d/K20redis-server -> ../init.d/redis-server
   /etc/rc6.d/K20redis-server -> ../init.d/redis-server
   /etc/rc2.d/S20redis-server -> ../init.d/redis-server
   /etc/rc3.d/S20redis-server -> ../init.d/redis-server
   /etc/rc4.d/S20redis-server -> ../init.d/redis-server
   /etc/rc5.d/S20redis-server -> ../init.d/redis-server

 

2,如果你想移除,使用以下方法

 

$ sudo update-rc.d -f redis-server remove
 Removing any system startup links for /etc/init.d/redis-server ...
   /etc/rc0.d/K20redis-server
   /etc/rc1.d/K20redis-server
   /etc/rc2.d/S20redis-server
   /etc/rc3.d/S20redis-server
   /etc/rc4.d/S20redis-server
   /etc/rc5.d/S20redis-server
   /etc/rc6.d/K20redis-server

 

 




Leave a Reply

Your email address will not be published. Required fields are marked *