#启停脚本
#!/bin/bash
# chkconfig: 2345 10 90
# description: Starts and Stops the redis daemon.
-- 复制
prog=redis
-- 将jinjia变量赋值给变量port
port={{port}}
-- 将配置文件赋值
conf="/usr/local/redis/etc/redis.conf"
-- redis的启动程序赋值
exec="/usr/local/redis/bin/redis-server"
-- 客户端程序命令赋值给stop;
-- -h {{host}} 连接的IP地址 ; -p {{port}} 连接的端口
stop="/usr/local/redis/bin/redis-cli -h {{addr}} -p $port shutdown"
pidfile=/var/run/redis_6379.pid
-- 赋值
retval=0
# source function library.
-- -f命令是shell中判断是否是普通文件的运算符,如果是,返回true
-- 判断functions是否是普通,是的话,执行该文件
-- ./ 用于执行文件,和sh 命令同效
if [ -f /etc/init.d/functions ]
then
. /etc/init.d/functions
fi
##############################################################################
start () {
-- 使用 redis-server 执行redis.conf; >/dev/null 执行结果的标准输出重定向到黑洞,不保留结果
-- 默认输出是标准输出,即1; 2>&1 表示标准错误等同于1,即也输出向黑洞
-- 最后一个& 表示后台运行该命令
${exec} $conf >/dev/null 2>&1 &
-- for循环
-- (())表示里面有运算式,可以进行运算
-- do done 循环体
for ((i=0; i<10; i++))
do
-- 将后面的命令执行的结果作为变量赋值给 pid
-- ps -ef 标准格式显示进程
-- | 通道符,可以认为是将前面的命令运行结果最为后面命令的 运行对象
-- grep 查询,搜索
-- grep grep 查询含有"grep"的行 ;查询过程本身也会生成一条进程
-- grep -v 对查询结果反选
-- ps -ef | grep -v grep | grep redis-server | grep 6379 查询redis的服务进程,并且包含有6379的,提出该查询自身的命令
-- awk '{print $2}' 筛选出每一条中的第二个变量,并打印出来
pid=$(ps -ef | grep -v grep | grep redis-server | grep 6379 | awk '{print $2}')
-- shell 中两个字符串直接写在一起,会自动串成一个
--"x$pid" != "x" 就是判断$pid是否为空
if [ "x$pid" != "x" ]
then
-- $pid > $pidfile 将 $pid替换文件$pidfile(redis.conf)中的内容
echo $pid > $pidfile
break
fi
-- 睡眠1秒
sleep 1
done
if [ -f $pidfile ]
then
-- cat 命令,将文件中的所有内容打印出来
pid=`cat $pidfile`
else
echo "$prog is stopped."
retval=1
return
fi
-- $? 显示最后命令的退出状态,0表示没有错误,其他表示有错误
-- -eq 等于
if [ $? -eq 0 ]
then
-- action是base的内置函数
-- 详解见:https://blog.youkuaiyun.com/ashic/article/details/52140294,可以自己百度下
action "Starting $prog pid: $pid" /bin/true
else
action "Starting $prog" /bin/false
retval=1
fi
}
stop ()
{
$stop
retval=$?
if [ $retval -eq 0 ]
then
action "Stopping $prog pid: $pid" /bin/true
-- 删除该文件夹及文件夹内所有内容
rm -rf $lockfile
else
action "Stopping $prog " /bin/true
retval=1
fi
}
restart() {
sleep 5
stop
start
}
reload() {
false
}
rh_status() {
status -p $pidfile $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
-- case 函数,判断输出的参数,来执行对应的start或者stop
case "$1" in
start)
rh_status_q && retval=0
$1
;;
stop)
rh_status_q || retval=0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
retval=2
esac
exit $retval
#!/bin/bash
# chkconfig: 2345 10 90
# description: Starts and Stops the redis daemon.
-- 复制
prog=redis
-- 将jinjia变量赋值给变量port
port={{port}}
-- 将配置文件赋值
conf="/usr/local/redis/etc/redis.conf"
-- redis的启动程序赋值
exec="/usr/local/redis/bin/redis-server"
-- 客户端程序命令赋值给stop;
-- -h {{host}} 连接的IP地址 ; -p {{port}} 连接的端口
stop="/usr/local/redis/bin/redis-cli -h {{addr}} -p $port shutdown"
pidfile=/var/run/redis_6379.pid
-- 赋值
retval=0
# source function library.
-- -f命令是shell中判断是否是普通文件的运算符,如果是,返回true
-- 判断functions是否是普通,是的话,执行该文件
-- ./ 用于执行文件,和sh 命令同效
if [ -f /etc/init.d/functions ]
then
. /etc/init.d/functions
fi
##############################################################################
start () {
-- 使用 redis-server 执行redis.conf; >/dev/null 执行结果的标准输出重定向到黑洞,不保留结果
-- 默认输出是标准输出,即1; 2>&1 表示标准错误等同于1,即也输出向黑洞
-- 最后一个& 表示后台运行该命令
${exec} $conf >/dev/null 2>&1 &
-- for循环
-- (())表示里面有运算式,可以进行运算
-- do done 循环体
for ((i=0; i<10; i++))
do
-- 将后面的命令执行的结果作为变量赋值给 pid
-- ps -ef 标准格式显示进程
-- | 通道符,可以认为是将前面的命令运行结果最为后面命令的 运行对象
-- grep 查询,搜索
-- grep grep 查询含有"grep"的行 ;查询过程本身也会生成一条进程
-- grep -v 对查询结果反选
-- ps -ef | grep -v grep | grep redis-server | grep 6379 查询redis的服务进程,并且包含有6379的,提出该查询自身的命令
-- awk '{print $2}' 筛选出每一条中的第二个变量,并打印出来
pid=$(ps -ef | grep -v grep | grep redis-server | grep 6379 | awk '{print $2}')
-- shell 中两个字符串直接写在一起,会自动串成一个
--"x$pid" != "x" 就是判断$pid是否为空
if [ "x$pid" != "x" ]
then
-- $pid > $pidfile 将 $pid替换文件$pidfile(redis.conf)中的内容
echo $pid > $pidfile
break
fi
-- 睡眠1秒
sleep 1
done
if [ -f $pidfile ]
then
-- cat 命令,将文件中的所有内容打印出来
pid=`cat $pidfile`
else
echo "$prog is stopped."
retval=1
return
fi
-- $? 显示最后命令的退出状态,0表示没有错误,其他表示有错误
-- -eq 等于
if [ $? -eq 0 ]
then
-- action是base的内置函数
-- 详解见:https://blog.youkuaiyun.com/ashic/article/details/52140294,可以自己百度下
action "Starting $prog pid: $pid" /bin/true
else
action "Starting $prog" /bin/false
retval=1
fi
}
stop ()
{
$stop
retval=$?
if [ $retval -eq 0 ]
then
action "Stopping $prog pid: $pid" /bin/true
-- 删除该文件夹及文件夹内所有内容
rm -rf $lockfile
else
action "Stopping $prog " /bin/true
retval=1
fi
}
restart() {
sleep 5
stop
start
}
reload() {
false
}
rh_status() {
status -p $pidfile $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
-- case 函数,判断输出的参数,来执行对应的start或者stop
case "$1" in
start)
rh_status_q && retval=0
$1
;;
stop)
rh_status_q || retval=0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
retval=2
esac
exit $retval