脚本的代码如下:
#!/usr/bin/perl
use strict;
# Startup/shutdown script for mysql-proxy
# Author: David Palmer
###############################################################
# CONFIGURATION #
###############################################################
# List of mysql hosts we want to spread our load out to
my $HOSTS = "10.134.121.181:3306,10.134.121.156:3306";
# Location of mysql-proxy on this box
my $MPROXY = "/usr/local/bin/mysql-proxy";
# PID file
my $PIDFILE = "/var/tmp/mproxy.pid";
# command
my $cmd = $ARGV[0];
die usage() unless defined $cmd;
if ($cmd eq "start") {
if (-f $PIDFILE) {
open(F,$PIDFILE) or die "crap: $!";
my $file = <F>;
print "Already started on: ", $file,"\n";
exit(-1);
} else {
start();
}
} elsif ($cmd eq "stop") {
if (! -f $PIDFILE) {
print "$MPROXY not running\n";
exit(-1);
} else {
stop();
}
} else {
print "i don't know how to do: $cmd\n";
exit(-1);
}
sub stop {
open(F,$PIDFILE) or die "$!";
my $pid = <F>;
$pid =~ s/\n//g;
close(F);
unlink($PIDFILE);
print "$MPROXY stopped [$pid]\n";
exec("kill -9 $pid");
exit(0);
}
sub start {
my $pid = fork();
die "fork failed!\n" unless defined $pid;
my @hosts = split(/\,/,$HOSTS);
my $cmd = $MPROXY;
foreach my $host (@hosts) {
$cmd .= " --proxy-backend-addresses=$host ";
}
if ($pid == 0) {
exec($cmd);
exit 0;
}
open(F,">$PIDFILE") or die "can't write to $PIDFILE: $!";
print F $pid, "\n";
close(F);
print "$MPROXY started [$pid]\n";
exit(0);
}
sub usage {
print "must provide: 'stop' or 'start' arguments\n";
exit(-1);
}



