php判斷l(xiāng)inux下程序問題實例
更新時間:2015年07月09日 15:38:33 作者:pooy
這篇文章主要介紹了php判斷l(xiāng)inux下程序問題,可有效的控制Linux下crontab控制程序定時執(zhí)行時資源調(diào)配問題,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php判斷l(xiāng)inux下程序問題。分享給大家供大家參考。具體如下:
有時候在服務器上面寫一些腳本的時候,經(jīng)常要放到crontab里面定時運行。時間長了就有一個問題,那就是程序重復運行消耗太多的資源,怎么處理呢?下面璞玉寫了兩種方法.
//第一種:用linux里面的正則匹配
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;
}
}
}
//調(diào)用:
if (ifrun('pooy',5))
{
die("pooy is running");
}
//備注:pooy是程序pooy.php的名稱!
//第二種:把進程寫到文件里面,然后用file函數(shù)去讀取然后去匹配字符串
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) {
//echo '"earth" not found in string';
$count[]='no';
break;
}
}
if(count($count) >= 1 )
{
echo "A same programs are running";
exit();
}else
{
echo "start__________________________________________________";
}
//注:"www/pooy" 是程序里面包含的字符串!
//現(xiàn)在php程序在linux運行是否通暢多了呢?
希望本文所述對大家的php程序設計有所幫助。
相關文章
PHP has encountered a Stack overflow問題解決方法
這篇文章主要介紹了PHP has encountered a Stack overflow問題解決方法,需要的朋友可以參考下2014-11-11
PHP數(shù)組的交集array_intersect(),array_intersect_assoc(),array_inte
求兩個數(shù)組的交集問題可以使用array_intersect(),array_inersect_assoc,array_intersect_key來實現(xiàn),其中array_intersect()函數(shù)是求兩個數(shù)的交集2011-05-05
PHP管理內(nèi)存函數(shù) memory_get_usage()使用介紹
我們在實際編碼中,要想實現(xiàn)對內(nèi)存的查看和操作,許多程序員們第一個想到的就是PHP memory_get_usage()這個PHP腳本內(nèi)存函數(shù)2012-09-09

