<div class="article_title" style="margin:5px 0px; font-size:20px; line-height:30px; font-family:'Microsoft YaHei'">
<h3 style="margin:0px; padding:0px; display:inline; font-weight:normal; font-size:20px; vertical-align:middle"> <span class="link_title"><a href="http://blog.csdn.net/charlie_2010/article/details/6266068" style="color:rgb(0,0,0); text-decoration:none">如何监视一个进程,崩溃挂掉后自动重启</a></span></h3>
</div>
<div class="article_manage" style="padding:5px 0px; color:rgb(153,153,153); line-height:24px; font-family:Arial; text-align:right">
<span class="link_postdate" style="margin:0px 5px 0px 0px">2011-03-21 20:53</span>
<span class="link_view" style="margin:0px 5px; padding:0px 0px 0px 14px" title="阅读次数">1783人阅读</span>
<span class="link_comments" style="margin:0px 5px; padding:0px 0px 0px 14px" title="评论次数"><a href="http://blog.csdn.net/charlie_2010/article/details/6266068#comments" style="color:rgb(51,102,153); text-decoration:none">评论</a>(0)</span>
<span class="link_collect" style="margin:0px 5px"><a href="" style="color:rgb(51,102,153); text-decoration:none" title="收藏">收藏</a></span>
<span class="link_report" style="margin:0px 5px"><a href="http://blog.csdn.net/charlie_2010/article/details/6266068#report" style="color:rgb(51,102,153); text-decoration:none" title="举报">举报</a></span>
</div>
<div class="tag2box" style="margin:10px 0px; color:rgb(51,51,51); font-family:Arial,Console,Verdana,'Courier New'">
<a href="http://blog.csdn.net/tag/details.html?tag=shell" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">shell</a>
<a href="http://blog.csdn.net/tag/details.html?tag=%e8%84%9a%e6%9c%ac" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">脚本</a>
<a href="http://blog.csdn.net/tag/details.html?tag=%e6%9c%8d%e5%8a%a1%e5%99%a8" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">服务器</a>
<a href="http://blog.csdn.net/tag/details.html?tag=bash" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">bash</a>
<a href="http://blog.csdn.net/tag/details.html?tag=path" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">path</a>
<a href="http://blog.csdn.net/tag/details.html?tag=windows" rel="noopener noreferrer" style="color:rgb(51,102,153); text-decoration:none; display:inline-block; padding:4px 10px; line-height:12px; margin-right:10px; border:1px solid rgb(238,238,238); background-color:rgb(238,238,238)" target="_blank">windows</a>
</div>
<div class="article_content" id="article_content" style="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51,51)">
<p>如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题。 </p>
<p> </p>
<p><strong><span style="font-size:14px">shell脚本</span></strong></p>
<p>下面的shell通过一个while-do循环,用ps -ef|grep 检查loader进程是否正在运行,如果没有运行,则启动,这样就保证了崩溃挂掉的进程重新被及时启动。</p>
<p>必须注意两点:</p>
<p>1、ps |grep 一个进程时必须加上其路劲,否则容易grep到错误的结果;</p>
<p>2、必须用 -v 从结果中去除grep命令自身,否则结果非空。</p>
<p> </p>
<div class="cnblogs_code">
<div>
<span style="color:rgb(0,128,0)"><span style="color:rgb(0,0,0)">#!/bin/sh<br> echo "Current DIR is " $PWD<br> while [ 1 ]<br> do<br> propserverpid=`ps -ef|grep propserver|grep 6668|grep -v grep|awk '{print $2}'`<br> if [ "$propserverpid" ]<br> then<br> echo "propserver is running,pid is " $propserverpid<br> kill -9 $propserverpid <br> else<br> echo "propserver is down ,now starting ..."<br> nohup $PWD/propserver -c config.ini -p 6668 &</span></span>
</div>
<div>
<span style="color:rgb(0,128,0)"><span style="color:rgb(0,0,0)"> fi<br> sleep 5<br> done<br> </span></span>
</div>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> 如果启动此shell时发现进程已经存在,说明以别的方式启动了进程而不是此shell,那么它会持续提醒找到进程,解决办法是,要么只用此shell启动服务,要么一经发现以其他方式启动的服务即kill掉,上面的语句就是这么干的:</p>
<p> </p>
<div class="cnblogs_code">
|
|