Domanda

I am currently using a php script to tail a log file on my server however depending on the software I am running the log file can be in a different location and have a different name. Current I have to manually adjust my script to accommodate for this.

Here is what I use for program one:

$cmd = "tail -75 /home/user/data.log";

Here is what I use for program two:

$cmd = "tail -75 /home/user/log/data2.log";

This is what my logtail script looks like currently:

<?php
$cmd = "tail -75 /home/user/data.log";
exec("$cmd 2>&1", $output);
$str = implode("\n", $output);
$patterns[0] = '0000000';
$replacements[0] = '';
$outputline = str_replace($patterns, $replacements, $str);
echo ("$outputline");
?>

Is it possible to setup the script to check to see which log file is being used instead of me just changing the script manually? It will always either be /home/user/data.log or /home/user/log/data2.log.

È stato utile?

Soluzione

Why not check for the file and then act accordingly?

if(file_exists("/home/user/data.log"))
  $cmd = "tail -75 /home/user/data.log";
else if(file_exists("/home/user/log/data2.log"))
  $cmd = "tail -75 /home/user/log/data2.log";
else
  die("No file to tail");

Altri suggerimenti

Why not tail them both, and not care which one is being used? :-)

tail -q fileA fileB 2> /dev/null
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top