Manging FastCGI Processes and Ensuring Uptime
Managing (Catalyst) deployments on Lighttpd with FastCGI can be tricky (at least as far as I can tell). You start the process, it gets assigned a PID and there it is. However, you start up other processes for other sites, the data center reboots your server, everything (tries to) comes back up, but which PID belongs to which site? Not-to-mention the occasional misfire of the start up script. What do you do?
My friend (and server mate) Russ had written a little shell script (see code below) to kill all the FastCGI processes and then restart them. This was for when one of us would update our various applications and would be ready to re-deploy.
So, I wrote a little script that uses Mech to check a site’s status. If the check does not return a code of 500 or 404, it just logs that all is fine. However, if a site responds with a code of 500, the script executes the shell script to kill all FastCGI processes and then restart them. If the the response is 404, the script restarts (in my case) Lighttpd.
Once in place, you just setup a little Cron job to run it (I run it every 15 min)
Here an example of Russ’ shell script
##########
pkill -f fcgi
pkill -f fcgi-pm
pkill -9 -f fcgi
pkill -9 -f fcgi-pm
/path/to/mysite.net/fastcgi.pl -l /tmp/mysite.socket -n 3 -d
/path/to/myothersite.com/fastcgi.pl -l /tmp/mysite.socket -n 3 -d
#######
And here is a sample script that I wrote to use under Ubuntu 7.10
########
#!/usr/bin/perl -w
# Written by Jay Varner 6/30/08, Atlanta, GA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use WWW::Mechanize;
use DateTime;
my @urls = (
"http://mysite.net",
"http://myothersite.org"
);
# Command to execute the webrestart shell script
my $fcgi_restart = "./webrestart";
# Command to restart Lighttpd (may vary by distro)
my $lighttpd_restart = "/etc/init.d/lighttpd restart";
# Set the current date and time for the log file using DateTime from CPAN
my $date_time = DateTime->now;
# Set path to the log file you want to use
my $log = '/path/to/log.txt';
# Loop through each of your sites
foreach my $site (@urls) {
# Get the Status using WWW::Mechanize from CPAN
my $mech = WWW::Mechanize->new();
$mech->get( $site );
my $status = $mech->status($site);
# If there is a server error, we will restart all the FastCGI processes.
if ($status == '500') {
system $fcgi_restart;
}
# If the site was not found, we will restart Lighttpd.
elsif ($status == '404') {
system $lighttpd_restart;
}
# Open the log file and record the status of each site and the current time.
open(DAT,">>$log") || die("Cannot Open File");
print DAT "$site || $status || $date_time \n";
close DAT;
}
######
Posted by jay On: Monday June, 2008 21:32
Comments
Hi guys, are your CMS up to date nowadays? Best Regards
nicccccccccccccccccce