I recently created a very simple PHP script to just check a server every minute and see if it’s online. I wasn’t too bothered about whether it was or not, but recently I’ve had issues connecting to it, and wanted to check if it was a problem with the box or my ISP.
Anyway, in the PHP script, I had some code
// if website is down
$log = fopen('uptime.log', 'a');
fwrite($log, 'SERVER IS DOWN @ ' . date('H:i:s l jS F') . "\n");
fclose($log);
If I ran this script via Firefox, it worked absolutely fine and logged it to the log file. I set up Cron to run the script every minute, and it appeared to run the script (I tested it with another script) fine, so I couldn’t see what the problem is.
After some Googling, and trying a few different solutions, I finally discovered that the problem was with fopen() and the path I was using
I had only referenced the file uptime.log when really it should have been as below
$log = fopen('/full/path/to/file/from/root/uptime.log', 'a');
Note how I have the full path to the file. I’m still not sure why it didn’t work before through cron, and it did through the browser, but all that matters is it works now.