#!/bin/bash
#*****************************************************
# Author: suixiaofeng
# blog:https:
//blog.cool360.org
# Email: 258818040@qq.com
# Last modified: 2017-06-30 19:34
# Filename: redis
# Description:
#****************************************************
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
redis_server=
"/u02/redis/bin/redis-server"
redis_conf=
"/u02/redis/conf/redis.conf"
[ -x /u02/redis/bin/redis-server -a -f /u02/redis/conf/redis.conf ] || {
echo
"redis is not install."
exit
1
}
if
[ $# -ne 1 ]
then
echo
$
"usage:$0 {start|stop|restart|status}"
exit
1
fi
function
start () {
stapro=`netstat -lntup|grep redis |wc -l`
if
[
$stapro
-ne 0 ] ; then
echo
-e
"\033[32mredis is running \033[0m"
exit
2
else
${redis_server} ${redis_conf}
sleep 2
stapro1=`netstat -lntup|grep redis |wc -l`
[
$stapro1
-ne 0 ] &&{
action
"redis is started"
/bin/true
exit
0
}
fi
}
function
stop () {
stopro=`netstat -lntup|grep redis |wc -l`
if
[
$stopro
-eq 0 ] ; then
echo
-e
"\033[32mredis is stopped \033[0m"
exit
2
else
# PID=`ps aux|grep redis|grep -v grep|awk
'{print $2}'
`
# kill -9 ${PID} >/dev/null
killproc redis-server
sleep 2
stopro1=`netstat -lntup|grep redis |wc -l`
[
$stopro1
-eq 0 ] &&{
action
"redis is stopped"
/bin/true
exit
0
}
fi
}
function
status () {
statpro=`netstat -lntup|grep redis |wc -l`
if
[
$statpro
-eq 0 ] ; then
echo
-e
"\033[32mredis is stopped \033[0m "
else
echo
-e
"\033[32mredis is running \033[0m "
fi
exit
0
}
case
$1
in
"start"
)
start
;;
"stop"
)
stop
;;
"restart"
)
stop
start
;;
"status"
)
status
;;
* )
echo
$
"Usage:$0 {start|stop|restart|status}"
exit
4
esac
exit
0
|
接着把脚本放在/etc/init.d下,赋权 chmod +x /etc/init.d/redis
操作如下:
[root@sf106232 srv]# /etc/init.d/redis status
redis is stopped
[root@sf106232 srv]# /etc/init.d/redis
usage:/etc/init.d/redis {start|stop|restart|status}
[root@sf106232 srv]# /etc/init.d/redis start
redis is started [ OK ]
[root@sf106232 srv]# /etc/init.d/redis status
redis is running
[root@sf106232 srv]# /etc/init.d/redis stop
redis is stopped [ OK ]
[root@sf106232 srv]# /etc/init.d/redis status
redis is stopped
[root@sf106232 srv]#
|
这个版本为最初的,可以参考标准的去完善。