一、 启动脚本模板:符合幂等性

  如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动

#!/bin/bash
instancename=
# check is instance running
PID=`ps -ef | $instancename | grep -v grep `
if [ ! -z "$PID" ]; then
echo "instance $instancename is running."
exit 0
fi # start instance
# TODO: start cmd # chenk whether instance be running by url or key word in logfile,choose one or check url
url=
loop=60
count=0
while $count < 60
do
curl $url && exit 0
sleep 1
count=$(($count + 1))
done
if [ $count -ge 60 ];then
echo "[ERROR]: Timeout ,failed."
exit 1
fi
echo "[INFO]: Instance $instancename started." # or check key word in logfile
keyword= xxx
logfile=
orgLineNum=`wc -1 $logfile | cut -d " " -f1`
loop=60
count=0
while $count < 60
do
endLineNum=`wc -1 $logfile | cut -d " " -f1`
deltaLine=$(($endLineNum - $orgLineNum))
tail -n $deltaLine $logfile | sed /$keyword/ && break
$orgLineNum=$endLineNum
sleep 1
done
if [ $count -ge 60 ];then
echo "[ERROR]: Timeout , failed."
exit 1
fi
echo "[INFO]: Instance $instancename started."  

二、停止脚本,符合幂等性

  可以重复调用

#!/bin/bash
instancename=
#check is instance running
PID=`ps -ef | grep $instancename | grep -v grep `
if [ -z "$PID" ];then
echo "instance $instancename is not running."
exit 0
fi # stop instance
# TODO : stop cmd # if stop cmd failed ,may kill or exit with error #or kill
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
echo "stop cmd failed , try to kill."
kill $PID
fi # if kill failed ,may kill -9
if [ ! -z "$PID" ];then
echo "kill process failed, try to kill -9."
kill -9 $PID
fi # or exit with error
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
echo "stop cmd failed."
exit 1
fi

  

shell启停服务脚本模板的更多相关文章

  1. 编写Redis启停服务脚本

    脚本内容如下; fi   esac   exit$RETVAL 下载脚本:艺搜下载 将下载下来的脚本放在/etc/init.d/目录下 更改脚本权限 chmod 777 /etc/init.d/red ...

  2. 编写Nginx启停服务脚本

    在/etc/init.d/目录下创建脚本 vim /etc/init.d/nginx 编写脚本内容:(其中下面2行需要根据情况自行修改) nginxd=/opt/nginx/sbin/nginx ng ...

  3. shell编程之服务脚本编写,文件锁以及信号捕获

    shell脚本编程是linux运维工程师必备的技能,也是非常重要的一个技能,所以把shell编程学好,只有好处.基础语法我也就不讲了,学过C语言这些语言的,稍微看一下就能明白shell编程的基础,所以 ...

  4. Redis windows版本的启停bat脚本命令

    Reids windows版本安装 redis windows官网推荐:https://github.com/MicrosoftArchive/redis/releases 下载解压即可. 启停bat ...

  5. MySQL - 启停服务

    Windows 环境 命令行方式 启动 MySQL 服务: net start mysql停止 MySQL 服务: net stop mysql 注:需要以管理员身份启动 cmd 后再执行上述命令. ...

  6. 04. 启停redis服务

    启动 查看redis.conf文件,可以通过general中的说明,配置通过systemd来启停redis和查看redis状态(作者没有采用,而是使用service管理,service配置参考< ...

  7. redis安装、配置、启停

    Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and ...

  8. liunx weblogic服务启停脚本

    #!/bin/bash #sh xx.sh start xx项目 例如:sh autoWeblogic.sh start bius #经测试发现weblogic 启动大概需要完全启动成功35秒左右 停 ...

  9. nginx启停脚本

    安装nginx时,源码包中未带官方的启动脚本,也就无法使用service nginxd start这种启动方式,查了下资料自己写了一个: #!/bin/bash #@version: #@author ...

随机推荐

  1. 关于第一次web前端面试的记录

    最近参加了一场面试,感觉自己题目都懂,但是说起来就是有点说不明白,所以写个博客整理以下吧.答案不少不是面试时回答的答案,只是整理一下可行答案 1. 如图1,使B相对于A垂直居中 图1 <styl ...

  2. wordpress翻译插件gtranslate

    https://www.gdstautoparts.com/

  3. js面向对象的几种方式----工厂模式、构造函数模式、原型模式

    对象的字面量 var obj={} 创建实例对象 var obj=new Object() 工厂模式 function cPerson(name,sex,age){ var o = new Objec ...

  4. JUC 一 线程池

    线程 线程,是程序执行的最小单元.线程是进程中的其中一个实体,是被系统独立调度和分派的基本单位 它可与同属一个进程的其它线程共享进程所拥有的全部资源. 一个线程可以创建和撤消另一个线程,同一进程中的多 ...

  5. redis笔记--------Jedis使用

    redis安装和启动就不说了 一.准备工作 1.redis -cli -p 6379 2.eclipse中新建项目,并导入jedis相关包 3.测试jedis连通性 二.Jedis常用API (哈希) ...

  6. (转)简单的RPC java实现 .

    转:http://blog.csdn.net/jackliang55/article/details/7580563 我也承认,RPC的名声大噪之时是在2003年,那一个“冲击波”病毒(Blaster ...

  7. 水一帖:快速计算ceil(a/b)的方式

    今天拍脑袋想出来的,不用ceil函数,不用浮点运算,不用取模,兼顾运行常数和代码量的向上取整除方法 在保证a,b>0时 ceil(a/b)=(a-1)/b+1; (完)

  8. P1613 跑路(倍增)

    P1613 跑路(倍增) 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十 ...

  9. 关于resin的一个错误,Resin 启动报错,访问页面500

    背景 客户集成javaagent报错,客户用的是resin,在本地复现问题,修改了bug,其中在resin中发布war包遇到的错误. 完整错误 500 [show] WEB-INF/web.xml:5 ...

  10. 剑指offer——11矩阵覆盖

    题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?   题解: 使用递归或者动态规划,明显,递归没有动态规划优 ...