早上好,
我有一个我一直在测试的PHP脚本,当我从命令行调用它时似乎运行正常。
我现在想通过cron自动化它,如何将我作为检查点放入文件的输出到日志文件中?
例如,我在脚本中有一些简单的echo命令,我希望输出显示在现有的日志文件中(以便它自动旋转等)
感谢,
格雷格
答案 0 :(得分:10)
运行Cron命令:
/path/to/php -f /path/to/script.php >> /path/to/logfile.txt
答案 1 :(得分:0)
尝试这样的事情:
<?php
function logToFile($filename, $msg)
{
$fd = fopen($filename, "a");
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
fwrite($fd, $str . "\n");
fclose($fd);
}
function logToMail($msg, $address)
{
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
mail($address, "Log message", $str);
}
function logToDB($msg, $type)
{
// open connection to database
$connection = mysql_connect("localhost", "joe", "pass") or die ("Unable to connect!");
mysql_select_db("logdb") or die ("Unable to select database!");
// formulate and execute query
$query = "INSERT INTO log (date, type, msg) VALUES(NOW(), '$type', '$msg')";
mysql_query($query) or die ("Error in query: $query. " .mysql_error());
// close connection
mysql_close($connection);
}
?>