Reputation: 75
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.
Upvotes: 1
Views: 72
Reputation: 207465
Why not tail them both, and not care which one is being used? :-)
tail -q fileA fileB 2> /dev/null
Upvotes: 1
Reputation: 10583
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");
Upvotes: 2