Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Keep FastCGI Processes Up and Running

by SouthFulcrum (Sexton)
on Jul 01, 2008 at 20:56 UTC ( #695027=snippet: print w/replies, xml ) Need Help??
Description: A little script that checks to see if a site is up based on the response code; a response of 500 executes a shell script that kills and restarts the FastCGI processes whereas a response of 404 restarts the Web server. Oh, and keeps a little log.
#!/usr/bin/perl -w

use strict;

use WWW::Mechanize;
use DateTime;

my @urls = (

# 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 C
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 pro
     if ($status == '500') {
          system $fcgi_restart;
          # Log this site failed and when
          open(DAT,">>$log") || die("Cannot Open File");
          print DAT "$site || $status || $date_time \n";
          close DAT;
     # If the site was not found, we will restart Lighttpd.
     elsif ($status == '404') {
          system $lighttpd_restart;
# Log that the sites were checked and when
open(DAT,">>$log") || die("Cannot Open File");
print DAT "Sites checked $date_time \n";
close DAT;
External shell script to kill and restart all FastCGI processes
# Thanks Russell Jurney <rjurney (at)>
pkill -f fcgi

pkill -f fcgi-pm

pkill -9 -f fcgi

pkill -9 -f fcgi-pm

/path/to/ -l /tmp/mysite.socket -n 3 -d

/path/to/ -l /tmp/mysite.socket -n 3 -d
Replies are listed 'Best First'.
Re: Keep FastCGI Processes Up and Running
by Tanktalus (Canon) on Jul 03, 2008 at 15:39 UTC

    Instead of running "./webrestart", I'd likely be tempted to do that in perl directly, especially so I could get a small delay between the pkills and the pkill -9's (say, a half-second, which not all 'sleep' commands can do) to give fastcgi time to close down cleanly before being brutally virtually murdered ;-)

    Also, I'm thinking that "pkill fcgi" is going to also kill fcgi-pm - after all, pkill is just grepping through the process list, and "fcgi" will match against "fcgi-pm" too. This leaves me with:

    use Time::HiRes qw(usleep); use File::Spec; # this should be near, if not combined with, the @urls list to encoura +ge keeping them in sync # which is far better than doing this in a separate file! # (you may be able to even just use the @urls array to generate this l +ist, even better!) my @sites = qw( /path/to/site/one /path/to/other/site ); sub fastcgi_restart { system qw(pkill fcgi); usleep(500); system qw(pkill -9 fcgi); for (@sites) { # probably should pick a unique name for the socket somehow, but I +'ll leave that # to you - I'm just copying your code here. system File::Spec->catfile($_, ''), qw(-l /tmp/ +cket -n 3 -d); } }
    Though, to be honest, I'd probably create an init script for this in my /etc/init.d, yes, probably in shell, and then just call /etc/init.d/fastcgi restart instead.

    Really cool, of course, would be to write an init script in perl that actually read your lighttpd configuration to determine the URLs and paths completely automatically, and could check the status, start, stop, and restart the fastcgi stuff. Then I'd have a cron job like this:

    #!/bin/sh if ! /etc/init.d/lighttpd status > /dev/null; then /etc/init.d/lighttpd status fi if ! /etc/init.d/fastcgi status > /dev/null; then /etc/init.d/fastcgi restart fi
    (This assumed that lighttpd's status worked the way you want/need, i.e., intutitively.)

    Just my two cents. :-)

    PS: ++ for trying to get your computer to do your work for you :-) It's a perfect example of Laziness.

Re: Keep FastCGI Processes Up and Running
by stonecolddevin (Vicar) on Sep 08, 2008 at 22:25 UTC

    I've always just done touch ~/domain/path/index.fcgi
    to "restart" my FCGI process, although that's only if you're not running an external fastcgi server.

    If you've got a host like Dreamhost that kills FCGI processes aggressively, try a curl call (using cron): */15 * * * * /usr/bin/curl -s 1>&2 &>/dev/null and that should keep your FCGI process alive.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://695027]
[Discipulus]: ah thanks.. 1nickt all is well with the new job?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-01-19 18:05 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (222 votes). Check out past polls.