How to save a daily log file in linux frpm a php script?

Im runing a script as a cron job every ten min. I want to create a daily log file for the results I thought its working but Im looking at monitor log and I only see one file for one date
why it is not creating a file for every day??


thank you!

$ourFileName = "/home/nurit/monitorTest/monitorLog"; 
	 $arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );
	 $ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
	 $currDate = date("d/m/y : H:i:s", time());
	 $file_contents = "\nThe server is down for: ".$host." on D:".$currDate;
	 fwrite($ourFileHandle,$file_contents);
	 fclose($ourFileHandle);

Open in new window

Nura111Asked:
Who is Participating?
 
Hugh McCurdyConnect With a Mentor Commented:
Here's my fixed code that I've tested.  This won't work for you as written.
<?php
$baseFileName = "log";
echo $baseFileName.date("Ymd")."\n";
$logFileName = $baseFileName . date("Ymd");
$arg = (file_exists($logFileName) == true ? "a+" : "w" );
echo $logFileName."\n";
echo $arg."\n";
$ourFileHandle = fopen($logFileName, $arg) or die("can't open file");
$currDate = date("d/m/y : H:i:s", time());
$file_contents = "\nThe server is down for: now on D:".$currDate."\n";
fwrite($ourFileHandle,$file_contents);
fclose($ourFileHandle);
?>

Open in new window

0
 
Hugh McCurdyCommented:
Try this script and it may show you wants wrong

<?php
$ourFileName = "foo.log";
echo $ourFileName.date("Ymd")."\n";
$arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );
echo $ourFileName.date("Ymd")."\n";
echo $arg."\n";
$ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
$currDate = date("d/m/y : H:i:s", time());
$file_contents = "\nThe server is down for: now on D:".$currDate."\n";
fwrite($ourFileHandle,$file_contents);
fclose($ourFileHandle);
?>

Open in new window


My modified script is checking for the file   foo.log20111007
but writes to  foo.log

Does it make sense now?
0
 
Nura111Author Commented:
not really... Do you know what wrong in my script?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Hugh McCurdyCommented:
Yes, I know what's wrong with the script.  I'm going to try to fix in the next post and give it to you for testing.  Standby please.
0
 
Hugh McCurdyCommented:
I believe this will work.  I tested it best I can without actually being on your computer.

$ourFileName = "/home/nurit/monitorTest/monitorLog".date("Ymd");
         $arg = (file_exists($ourFileName) == true ? "a+" : "w" );
         $ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
         $currDate = date("d/m/y : H:i:s", time());
         $file_contents = "\nThe server is down for: ".$host." on D:".$currDate;
         fwrite($ourFileHandle,$file_contents);
         fclose($ourFileHandle);

Open in new window

0
 
Hugh McCurdyCommented:
You were testing against /home/nurit/monitorTest/monitorLog20111007 but then actually writing to /home/nurit/monitorTest/monitor

0
 
Hugh McCurdyCommented:
To be clear (I hope), I changed the first 2 lines.  The other lines remain the same.
0
 
Nura111Author Commented:
Do you mean to

You were testing against /home/nurit/monitorTest/monitorLog20111007 but then actually writing to /home/nurit/monitorTest/monitorLog ?

oh I get it cant believe it! :)
so by the way why every time its write to monitorLog its erased the dormer content isnt that suppose to add it on top of that?

Im trying really quick you suggestion to see if its work
0
 
Hugh McCurdyCommented:
Yes, that's what I found.  It was overwriting.

Sometimes it helps just to have another set of eyes.


Note, it should be safe to always use mode 'a' to fopen even if the file doesn't exist.

http://php.net/manual/en/function.fopen.php

'a'       Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

For a freshly created file, the end of the file is also the beginning of the file.
0
 
Nura111Author Commented:
"so by the way why every time its write to monitorLog its erased the dormer content isnt that suppose to add it on top of that?"

So you think its because I opened it with a+ and not a?

0
 
Hugh McCurdyCommented:
Yes, since the file it was looking for never existed,    $arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );     would always return 'w'

It never returned 'a+'


You don't need a+ since you aren't reading the file (right?).  A simple 'a' should do the trick.  But a+ isn't broken.
0
 
Hugh McCurdyCommented:
Glad I could be of assistance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.