当批处理文件作为cronjob

时间:2015-04-24 05:54:05

标签: php windows batch-file logging cron

我在我的Windows 7机器上运行批处理文件作为cronJob,我想要的是当cron Job与数据一起运行时,我想创建一个日志文件,它在控制台中显示。 数据是我在批处理文件中导入的index.php中的echo语句。 帮我解决这个问题。 的index.php

<?php
echo "Welcome" ;

$fileD = "Login_".date('Y-m-d').".csv";
$fp1 = fopen($fileD, 'a+');

//Getting the files from below mentioned folder
$iterator1 = new FilesystemIterator("C:/wamp/www/logs1");
$iterator2 = new FilesystemIterator("C:/wamp/www/logs2");
$filelist = array();
foreach($iterator1 as $GLOBALS['entry1']) 
{
    if (strpos($entry1->getFilename(), "p1") === 0) 
    {
        $filelist[] = $entry1->getFilename();
   echo $entry1;
    }
}
foreach($iterator2 as $GLOBALS['entry2']) {
    if (strpos($entry2->getFilename(), "p2") === 0) {
        $filelist[] = $entry2->getFilename();
    echo "<br>";
echo $entry2;
    }
}


$file1 = file_get_contents($entry1);
fwrite($fp1, $file1);

$file1 = file_get_contents($entry2);
fwrite($fp1, $file1); 
fclose($fp1);
echo "<br/>";
echo "Done";
 echo "<br/>";
 //Deletes log file present in the logs folder
$n1= "$entry1";
if(!unlink($n1))
 {
  echo ("Error deleting file1 $n1");
  }
   else
   {
   echo ("Deleted $n1");
  }
   echo "<br/>";

  $n2= "$entry2";
  if(!unlink($n2))
  {
  echo ("Error deleting file2 $n2");
   }
     else
  {
     echo ("Deleted $n2");
   }

 echo "<br/>";
    foreach (glob("*.csv") as $filename) 
    {
        echo "$filename size " . filesize($filename) . "\n";
        echo "<br>";

    }

echo "<br>";

//$insertionDate = substr($filename,6,10);

$servername = "localhost";
$username = "user";
$password = "";
$dbname = "stat";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$file = file_get_contents($fileD);
$count = preg_match_all("/,Login,/", $file, $matches);



echo "Csv first word ";
$insertionDate = substr($file,1,10);

echo "<br/>";
echo "Total Hits:" . $totalLines = count(file($fileD));
echo "<br/>";
echo "Login:" . $count;


// Insert the Total hits and the corresponding success and failure count
$sql = "INSERT INTO hit_s (HitDate, count, category,success,failure,tcount,ocount)
VALUES ('$insertionDate', $totalLines, 'Hits',$success,$fail,$treeCnt,$oCnt)";

if ($conn->query($sql) === TRUE) {
    echo "Total hits record inserted successfully \n";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}




$iterator = new FilesystemIterator("C:/wamp/www/Fed");
$filelist1 = array();
foreach($iterator as $GLOBALS['entry3']) 
{
    if (strpos($GLOBALS['entry3']->getFilename(), "*.csv") === 0) 
    {
        $filelist1[] = $GLOBALS['entry3']->getFilename();

    }

}
echo $GLOBALS['entry3']; 


echo "<br/>";
$entry3="$fileD";
$n3= "$entry3";
 if(!unlink($n3))
  {
  echo ("Error deleting $n3");
  }
else
  {
  echo ("Deleted $n3");
  }
  echo "<br/>";


$conn->close();


?>

在批处理文件中调用index.php文件,如下所示 C:\ wamp \ bin \ php \ php5.4.16 \ php.exe C:\ wamp \ www \ Fed \ csv \ index.php

1 个答案:

答案 0 :(得分:0)

看起来syslog对您有用:

$access = date("Y/m/d H:i:s");
syslog(LOG_WARNING, "Unauthorized client: $access {$_SERVER['REMOTE_ADDR']} ({$_SERVER['HTTP_USER_AGENT']);
相关问题