我有个问题。我有一个脚本,在 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 ,我发现邮件命令在 45 秒延迟后执行。据我了解,我需要运行带有文件发送的主块,并在延迟后(要写入日志)获取日志内容。那么我该怎么做呢?