Run PHP script via cron, how can I log any output?

Morning everything

I have a php script that I tested and it seems to work fine when I call it from the command line.

Now I want to automate it through cron, how can I get the output that I put in the file as breakpoints in the log file?

for example, I have some simple echo commands in a script, and I would like the result to be displayed inside an existing log file (so that it automatically rotates, etc.)

thanks,

Greg

+6
php logging cron
source share
2 answers

Cron command to run:

/path/to/php -f /path/to/script.php >> /path/to/logfile.txt 
+6
source share

Try something like this:

 <?php function logToFile($filename, $msg) { $fd = fopen($filename, "a"); $str = "[" . date("Y/m/dh:i:s", mktime()) . "] " . $msg; fwrite($fd, $str . "\n"); fclose($fd); } function logToMail($msg, $address) { $str = "[" . date("Y/m/dh: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); } ?> 
0
source share

All Articles