让脚本的一部分继续运行

时间:2013-04-23 10:12:41

标签: php

我有一个问题。 我有一个脚本,在N次之后将文件发送到电子邮件(从crontab执行) 问题是,我尝试从maillog到mysql输出。

这是一个脚本

#!/usr/bin/php
<?php
date_default_timezone_set("Europe/Chisinau");
$faxdir = '/var/spool/fax/';
$dir = opendir("/var/spool/fax/");
while(($file = readdir($dir)) !== false){
if (($file != ".") && ($file != "..")){
    $mytime = date('d-m-Y H:i:s');
    $sendfile = $file;
    $findcaller = explode('_', $file);
    $findcaller = explode('-', $file);
    $findsrc = shell_exec('echo "'.$findcaller[0].'" | sed "s/[^_]*\_//"');
    $findsrc = str_replace(array("\r","\n"),"",$findsrc);
    $caller = shell_exec('echo "'.$findcaller[1].'" | sed "s/[^_]*\_//"');
    $cutbillid = shell_exec('echo "'.$file.'" | sed "s/[^_]*\_//"');
    $findnumber = shell_exec('echo "'.$cutbillid.'" | sed "s/[^-]*\-//"');
    $findnumber=str_replace(array("\r","\n"),"",$findnumber);
    $key = '.TIF';
    $number=preg_replace('/'.$key.'.*/','',$findnumber);
    $showstatus = shell_exec('/usr/sbin/lsof | grep "'.$faxdir.'"*"'.$number.'"');

    if ($showstatus <> ""){
        exit(0);
    }else{
        $db_link = mysql_connect("localhost", "root", "") 
            or die("Could not connect: " . mysql_error());
        mysql_select_db("centrexdb");
        $query = mysql_query("SELECT `fax2email` FROM users WHERE username='".$number."' AND faxstatus='1'");
        $fax2email = mysql_fetch_array($query);
        if ($fax2email[0] == ''){
            $currdate = shell_exec('echo "'. date('Y-m-d_H:i:s') .'"');
    $noemail = shell_exec('mv /var/spool/fax/"'.$file.'" /var/spool/fax-nomail/"'.$currdate.'"--"'.$file.'"');
        } else {
            $findcaller = explode('_', $file);
            $findcaller = explode('-', $file);
            $caller = shell_exec('echo "'.$findcaller[1].'" | sed "s/[^_]*\_//"');
            $caller=str_replace(array("\r","\n"),"",$caller);
$sendmail = shell_exec('echo | mutt -a "'.$faxdir.'""'.$sendfile.'" -s "Fax From '.$findsrc.' to '.$number.'" "'.$fax2email[0].'"');
echo "executing";
        unlink(''.$faxdir.''.$sendfile.'');
        }
        mysql_close($db_link);
    }
}
sleep(45);
$dbnew = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("FAX");
$myresult=shell_exec('cat /var/log/maillog |grep "to=<'.$fax2email[0].'>" |tail -n1');
echo $myresult;
$currentdate = shell_exec('echo "'. date('Y-m-d_H:i:s') .'"');
$umail = $fax2email[0];
mysql_query("INSERT into faxlog (Date, UserNumber, SourceNumber, UsedEmail, MailLog)     VALUES('$mytime', '$number', '$findsrc',
'$umail', '$myresult')");
mysql_close($dbnew);
sleep(5);
}

exit(0);
?>

问题是如果在文件夹中有多个文件,则第一个文件脚本从日志过去值返回,因为日志需要一个时间来写入。我做了tail -f / var / log / maillog,我发现邮件命令在45s延迟后执行。据我所知,我需要运行带有文件发送的主块,并在延迟(要写入日志)后获取日志内容。 那我该怎么办?

0 个答案:

没有答案