Linux之的Shell腳本編寫啓動Redis服務

我的redis:

服務路徑:/usr/local/redis/bin/redis-server
配置文件路徑:/usr/local/redis/bin/redis.conf

1、先創建一個文件vim redis.sh

2、然後是編寫shell srcipt,保存退出

3、增加腳本可執行權限, chmod +x redis.sh

4、最後是執行sh腳本,./redis.sh start

5、查詢進程,ps -aux | grep redis

 

#!/bin/bash
# description:  Redis is a persistent key-value database
#定義變量
#PORT=6379
NAME="redis-server"
pid=$(ps -ef|grep $NAME |grep -v grep |awk '{print $2}')
#CHECK_PORT=`netstat -tnlp|grep "\b$PORT\b"`
PID_FILE=/var/run/redis_6379.pid
REDIS_CONFIG=/usr/local/redis/bin/redis.conf
REDIS_SERVER=/usr/local/redis/bin/redis-server
RETAVL=0
#檢查shelk公共函數庫是否存在,存在就加載
FUNCTIONS_PATH=/etc/init.d/functions
[ -f $FUNCTIONS_PATH ]&& source $FUNCTIONS_PATH
#檢查redis文件是否存在並可執行
[ -x $REDIS_SERVER ]|| exit 0
 
#定義函數
#檢查是否執行成功
check(){
	RETAVL=$?
	if 
		[ $RETAVL -eq 0 ];then
		action "redis is $1" /bin/true
	else
		action "redis is $1" /bin/false	
	fi
}
#啓動服務
start(){
	if
		[ -f $PID_FILE  ];then
		echo "redis already running"
	else
		$REDIS_SERVER $REDIS_CONFIG
		check started		
	fi
	return $RETAVL
	
}
#停止服務
stop(){
	if
		[ ! -f $PID_FILE  ];then
		echo "redis is not running."
	else
		for id in $ID
			do
				kill -9 $id
			done
		check stopped		
	fi
 
}
 
#重啓服務
restart(){
	stop
	sleep 2
	start
	
 
}
 
#判斷
case "$1" in
start)
		start
		;;
stop)
		stop
		;;
restart)
		restart
		;;
*)
		echo $"Usage:$0{start|stop|restart|help}"
esac
exit $RETAVL

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章