本文实例讲述了php判断linux下程序问题。分享给大家供大家参考。具体如下:
有时候在服务器上面写一些脚本的时候,经常要放到crontab里面定时运行。时间长了就有一个问题,那就是程序重复运行消耗太多的资源,怎么处理呢?下面璞玉写了两种方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | function ifrun( $clsname , $bf = 0)
{
$str =shell_exec( "/bin/ps ax > /home/root/" . $clsname . "_run.txt" );
$str =shell_exec( "/bin/grep -c '" . $clsname . ".php' /home/root/" . $clsname . "_run.txt" );
if ( $bf >0)
{
if ( $str >= $bf )
{
return 1;
}
else
{
return 0;
}
}
else
{
if ( $str >=2)
{
return 1;
}
else
{
return 0;
}
}
}
if (ifrun( 'pooy' ,5))
{
die ( "pooy is running" );
}
system( 'ps -ef |grep wget > /root/pooy.txt' );
$arr =file( '/root/pooy.txt' );
$total = count ( $arr );
for ( $i =0; $i < $total ; $i ++){
$count = array ();
if ( stristr ( $arr [ $i ], 'www/pooy' ) !== FALSE) {
$count []= 'no' ;
break ;
}
}
if ( count ( $count ) >= 1 )
{
echo "A same programs are running" ;
exit ();
} else
{
echo "start__________________________________________________" ;
}
|
ログイン後にコピー
希望本文所述对大家的php程序设计有所帮助。