I compiled a mysql tarball and installed it into /usr/local instead of
using my vendor's package. While doing that, I noticed the mysqld_safe
shell script does not have any limit on failure restarts.
If mysql dies and restarts more than say, X times in Y seconds, it could
hog the CPU, having little or no hope of stable operation. So I wrote a
script to limit the number of restarts within a given time interval.
I used Perl since it can grab the current time without spawing an extra
subshell. And I used the list replication (cool feature) of Perl to
initialize all elements of an array to the same value, without needing a
for loop.
Since I know where the mysqld binary resides in my filesystem, I don't
need all the extra gook mysqld_safe does, so I whittled it down to the
bare essential restart code.
#!/usr/bin/perl
use strict;
use warnings;
use FileHandle;
use Getopt::Long;
use File::Basename;
use File::Temp ();
STDOUT->autoflush (1);
STDERR->autoflush (1);
my ($tb, $MYSQL_BIN, $MYSQL_PID, $MYSQL_SOCK);
$tb = GetOptions (
'b=s' => \$MYSQL_BIN,
'p=s' => \$MYSQL_PID,
's=s' => \$MYSQL_SOCK
);
unless ($tb && $MYSQL_BIN && $MYSQL_PID && $MYSQL_SOCK) {
print "Usage: ", basename ($0), " -b binary -p pid -s sock\n";
exit 1;
}
$ENV{'TMPDIR'} =
File::Temp::tempdir ('mysql.XXXXXX', DIR => '/tmp', CLEANUP => 1);
my (@mark, $limit, $interval, $now);
$limit = 7;
$interval = 200;
@mark = (-$interval) x ($limit + 1);
$now = $limit;
while (1) {
unlink $MYSQL_PID, $MYSQL_SOCK;
system $MYSQL_BIN, "--pid-file=$MYSQL_PID";
last unless -e $MYSQL_PID;
$now = 0 unless ++$now < $limit;
$mark[$now] = time ();
$mark[$limit] = $mark[$now] if $now == 0;
if ($mark[$now] - $mark[$now + 1] < $interval) {
print "failure exceeding restart tolerance\n";
last;
}
}
--
Web mail, POP3, and SMTP
http://www.beewyz.com/freeaccounts.php
|