<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post/4172/</link>
<title><![CDATA[crontab 不能执行的原因总结,和查杀Crontab服务的僵尸进程方法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Sat, 02 Apr 2011 14:39:32 +0000</pubDate> 
<guid>http://jackxiang.com/post/4172/</guid> 
<description>
<![CDATA[ 
	最近经常碰到关于crontab不能执行的，初步总结了有以下几个原因：<br/>第一，脚本的原因：大多数情况下，我们要相信科学，相信计算机，不是有鬼，就是我们的脚本的问题，这种问题导致crontab不能执行的概率占到70%以上。因为程序执行到某一步导致crontab终止执行，我就碰到一次在迁移代码的时候将数据库连错了。导致无法访问而死在那里了。<br/>第二，执行环境问题，当我们碰到第一情况下，一般都可以通过手动执行程序将问题扼杀在摇篮里，一般情况下高手是不应该犯第一种错误的。问题是当我们手动执行成功而crontab不能执行的时候，笔者碰到一次就是执行环境的问题，例如相关路径的设置问题。解决方案：在代码最前面执行 source /home/user/.bash_profile<br/>第三，系统时间不正确。这种问题最好理解，也是比较常见和隐蔽的问题，解决方案：date -s ********<br/>第四，就是我们的脚本是否有可执行权限。必须保证执行脚本的用户有执行改文件的权限。<br/>第五，crontab 守护进程死掉了。这种情况是极少发生的，但也不排除，当我们实在是找不到其他原因的时候可以用。解决方案：重启该进程。<br/>另外，介绍大家一个关于如何查看crontab最修修改时间的方法：<br/>进入目录/var/spool/cron/里面会有N个用户名为文件名的文件，只要建立过crontab的用户在这里都会有以该用户名为文件名的文件，该文件的最后修改时间就是该用户的的crontab的最后修改时间。just do it<br/><br/><br/>有时候前置机会出现死进程等，做了这个自动查杀的脚本，已在sco unix505中测试通过，如想测试，可ps -efl &gt;file ,自行修改file里的进程状态及占用时间，再将脚本的输入源由ps -efl改为从file得到，屏敝 kill -9 $pid, 即可通过tty输出及日志文件观察到正确结果<br/>请大家斧正.谢谢！<br/>（如将此脚本加入crontab，时间自定，即可达到自动查杀超时或僵死的进程）<br/><div class="code"><br/>#!/bin/ksh<br/>#自动查杀超时或僵死的进程的脚本&nbsp;&nbsp; V050930<br/>#crontab -e&nbsp;&nbsp;<br/>#0,5,10,15,20,25,30,40,45,50,55 * * * * /usr/bin/autokill.sh &amp;&nbsp;&nbsp;#设置为每5分钟检查一次超时进程<br/>#当然简写成这样也成：<br/>*/5 * * * * * /usr/bin/autokill.sh &amp;&nbsp;&nbsp;#设置为每5分钟检查一次超时进程<br/>#为了安全保险起见,此版脚本的清除范围为:由终端启动的,占用CUP时间超过指定时间长度的,非root用户的进程或僵尸进程&nbsp;&nbsp;^_^<br/>#检测参数<br/>killlog=&quot;/tmp/kill.log&quot;&nbsp;&nbsp;&nbsp;&nbsp;#默认自动清除超时进程或僵死进程的日志<br/>out=60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 默认的超时时间，以秒为单位，默认为60秒，范围为10秒--36000秒<br/>trap &#039;rm /tmp/kill.tmp 2&gt;/dev/null&#039; 0 1 2 3 9 15<br/>test $LOGNAME != root &amp;&amp; &#123; echo &quot;Sorry ! 本 脚 本 只 能 由 root 操 作 !&#92;n&#92;n&quot; ; exit 1 ; &#125;<br/>test &quot;$out&quot; &#124;&#124; &#123; echo &quot;&#92;n&nbsp;&nbsp;超时时限不能为空&#92;n&quot; ; exit 1 ; &#125;<br/>test &quot;$(echo $out &#124; sed -n &#039;/^&#91;0-9&#93;&#91;0-9&#93;*$/p&#039;)&quot; &#124;&#124; &#123; echo &quot;&#92;n&nbsp;&nbsp;超时时限只能为整数值&#92;n&quot; ; exit 1 ; &#125;<br/>test $out -ge 10 -a $out -le 36000 &#124;&#124; &#123; echo &quot;&#92;n&nbsp;&nbsp;超时时限范围为10秒--36000秒&#92;n&quot; ; exit 1 ; &#125;<br/>#查找超时或僵尸进程<br/>ps -efl &#124;awk -v outtest=$out &#039;&#123; outtime=timetest($14) &#125;<br/>$2~/Z/ &#124;&#124; ( $3!~/root/ &amp;&amp; $13!~/&#92;?/ &amp;&amp; outtime==&quot;outtime&quot; ) &#92;<br/>&#123;print $3,$4,$13,$2,$14,$15 &#125;<br/>function timetest(ot)<br/>&#123;<br/>hour=substr(ot,1,2)<br/>min=substr(ot,4,2)<br/>sec=substr(ot,7,8)<br/>if ( hour*3600+min*60+sec &gt; outtest)<br/>return &quot;outtime&quot;<br/>else<br/>return &quot;good&quot; <br/>&#125; &#039; &gt;/tmp/kill.tmp 2&gt;/dev/null<br/>#保存清除列表<br/>if &#91; -s /tmp/kill.tmp &#93;<br/>then<br/>pass=0 <br/>error=0<br/>echo &quot;&#92;n清 除 时 间:&nbsp;&nbsp;&quot;$(date +%y/%m/%d-%H:%M:%S)&quot;&#92;n&quot; &gt;&gt; $killlog<br/>awk &#039;BEGIN&#123;printf(&quot;%-8s%-8s%-8s%-7s%-10s%-30s&#92;n&quot;),&quot;用户名&quot;,&quot;进程号&quot;,&quot;终端号&quot;,&quot;状态&quot;,&quot;占用时间&quot;,&quot;任务名&quot;&#125; <br/>&#123; state=statetest($4) ; printf(&quot;%-8s%-8s%-8s%-7s%-10s%-30s&#92;n&quot;),$1,$2,$3,state,$5,$6&#125; END&#123;print &quot;&#92;n&quot;&#125;<br/>function statetest(test)<br/>&#123;&nbsp;&nbsp;<br/> if (test==&quot;S&quot;) return &quot;睡眠&quot;<br/> if (test==&quot;R&quot;) return &quot;运行&quot;<br/> if (test==&quot;Z&quot;) return &quot;僵尸&quot;<br/> if (test==&quot;O&quot;) return &quot;不存在&quot;<br/> if (test==&quot;B&quot;) return &quot;等待&quot;<br/> if (test==&quot;T&quot;) return &quot;停止&quot;<br/> if (test==&quot;I&quot;) return &quot;中间&quot;<br/>&#125;&#039; /tmp/kill.tmp &gt;&gt; $killlog 2&gt;/dev/null<br/>#清除进程<br/>for pid in $(awk &#039;&#123;print $2&#125;&#039; /tmp/kill.tmp &#124;sort -rn) <br/>do <br/>kill -9 $pid&nbsp;&nbsp;<br/>test $? -eq 0 &amp;&amp; &#92;<br/>&#123; echo &quot;自动清除进程 $pid 成功&quot; &#124; awk &#039;&#123;printf(&quot;&#92;n%-14s%-10s%-4s&quot;),$1,$2,$3&#125;&#039; ; <br/>echo &quot;自动清除进程 $pid 成功&quot;&nbsp;&nbsp; &#124; awk &#039;&#123;printf(&quot;%-14s%-10s%-4s&#92;n&quot;),$1,$2,$3&#125;&#039; &#92;<br/>&gt;&gt; $killlog 2&gt;/dev/null ;&nbsp;&nbsp;pass=$((pass+1)) ; &#125; &#124;&#124; &#92;<br/>&#123; echo &quot;自动清除进程 $pid 失败&quot; &#124; awk &#039;&#123;printf(&quot;&#92;n%-14s%-10s%-4s&quot;),$1,$2,$3&#125;&#039; ; <br/>echo &quot;自动清除进程 $pid 失败&quot;&nbsp;&nbsp; &#124; awk &#039;&#123;printf(&quot;%-14s%-10s%-4s&#92;n&quot;),$1,$2,$3&#125;&#039; &#92;<br/>&gt;&gt; $killlog 2&gt;/dev/null ; error=$((error+1)) ; &#125;<br/>done<br/>#保存最终统计结果<br/>test $error -eq 0 &amp;&amp; &#92;<br/>&#123; echo &quot;此次共清除 $&#123;pass&#125; 个进程成功&quot; &#124; awk &#039;&#123;printf(&quot;&#92;n&#92;n%-14s%-4s%-10s&#92;n&#92;n&quot;),$1,$2,$3&#125;&#039;<br/>&nbsp;&nbsp;echo &quot;此次共清除 $&#123;pass&#125; 个进程成功&quot; &#124; awk &#039;&#123;printf(&quot;&#92;n%-14s%-4s%-10s&#92;n&#92;n&quot;),$1,$2,$3&#125;&#039; &#92;<br/>&gt;&gt; $killlog 2&gt;/dev/null ; &#125; &#124;&#124; &#92;<br/>&#123; echo &quot;此次共清除 $&#123;pass&#125; 个进程成功 $&#123;error&#125; 个进程失败&quot; &#124; &#92;<br/>awk &#039;&#123;printf(&quot;&#92;n&#92;n%-14s%-4s%-16s%-10s%-10s&#92;n&#92;n&quot;),$1,$2,$3,$4,$5&#125;&#039; ; <br/>&nbsp;&nbsp;echo &quot;此次共清除 $&#123;pass&#125; 个进程成功 $&#123;error&#125; 个进程失败&quot; &#124; &#92;<br/>awk &#039;&#123;printf(&quot;&#92;n&#92;n%-14s%-4s%-16s%-10s%-10s&#92;n&#92;n&quot;),$1,$2,$3,$4,$5&#125;&#039; &gt;&gt; $killlog 2&gt;/dev/null ; &#125;<br/>fi<br/></div><br/><br/>这个是UNix上写的shell，不知在Linux上可以用不，值得探究。<br/>来源：http://hi.baidu.com/zengyinfei/blog/item/6c2232164d239c4220a4e980.html<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.oschina.net/bbs/thread/6561?lp=193
]]>
</description>
</item><item>
<link>http://jackxiang.com/post/4172/#blogcomment63904</link>
<title><![CDATA[[评论] crontab 不能执行的原因总结,和查杀Crontab服务的僵尸进程方法]]></title> 
<author>三江小渡 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 19 Aug 2013 09:18:27 +0000</pubDate> 
<guid>http://jackxiang.com/post/4172/#blogcomment63904</guid> 
<description>
<![CDATA[ 
	#0,5,10,15,20,25,30,40,45,50,55 * * * * /usr/bin/autokill.sh &amp;&nbsp;&nbsp;#设置为每5分钟检查一次超时进程这句改成 */5 * * * * /usr/bin/autokill.sh &amp;&nbsp;&nbsp;吧。。。。
]]>
</description>
</item><item>
<link>http://jackxiang.com/post/4172/#blogcomment63905</link>
<title><![CDATA[[评论] crontab 不能执行的原因总结,和查杀Crontab服务的僵尸进程方法]]></title> 
<author>root &lt;admin@yourname.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 19 Aug 2013 12:48:31 +0000</pubDate> 
<guid>http://jackxiang.com/post/4172/#blogcomment63905</guid> 
<description>
<![CDATA[ 
	好，多谢提醒。
]]>
</description>
</item>
</channel>
</rss>