Forum Moderators: coopster

Message Too Old, No Replies

Checking if filestamp is less than 1 minute

         

csdude55

9:47 pm on Feb 7, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use the following to see if MySQL is responding. If not then check to see if the timestamp of a text file is less than 1 minute ago; if so then I restart MySQL, then touch the file to update the timestamp.

$dbh = @mysqli_connect('localhost', $user, $pass, $database);

if (!$dbh) {
if (time() - filemtime('/home/account/data/mysql_restart.dat') > 60) {
mail('me@gmail.com',
'MySQL Restarted',
mysqli_connect_error() . "\n\n" .
'IP: ' . $_SERVER['REMOTE_ADDR']);

exec('/etc/rc.d/init.d/mysql restart');
touch('/home/account/data/mysql_restart.dat');
}

exit;
}


At 3:52am, though, I received 52 emails! So it appears that time() - filemtime(...) > 60 didn't work as expected. The $_SERVER['REMOTE_ADDR'] for each of them was different so it doesn't appear to be an issue of browser caching.

I tested the timestamp of the file using date('YmdHis', filemtime(...)), and the actual timestamp is 035219. So it appears that those 52 emails were sent in less than 19s.

Considering we're talking about 3:52am, I'm guessing that these are bots that are hitting faster than the system can modify the text file.

Any suggestions on what I can do to prevent the system from trying to restart MySQL 52 times in 19 seconds?

phranque

9:58 pm on Feb 7, 2023 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



touch the file first and then restart MySQL.

csdude55

6:15 am on Feb 8, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Wouldn't that risk resetting the timer even if the MySQL restart failed? I guess that's not a huge deal, though; worst case scenario it's unresponsive for 60 whole seconds before trying again.

Dimitri

11:04 am on Feb 18, 2023 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Use clearstatcache , before calling the filemtime or after the touch function. [php.net...]