Search code examples
perlfastcgidancer

Killing Long Running Fastcgi Processes


I have a Perl Dancer web application which uses the mod_fastcgi serving method from Apache2. The application has to accept uploaded files. When a user is uploading a file and presses the stop button, the fastcgi process hangs, running at 100% until I manually kill the process.

Is there any setting that can automatically kill a process that has hung like this? Is there any way to automatically kill a fastcgi process that has been running for a certain amount of time?


Solution

  • Since the function I'm interested in isn't an option with mod_fastcgi and I can't seem to find the portion of the code to wrap in Time::Out to kill the process. I thought I would share my hacked ttogether solution.

    I searched for a single linux command to do this, but killall didn't work (it wouldn't specifically find just the perl command running that server instance) and pkill didn't either (couldn't specify an age of the process to kill).

    So I wrote a short perl script, which is run as root, to kill jobs with the correct name and age of dancer mod_fastcgi server instances:

    #!/usr/bin/perl -w
    
    use Proc::ProcessTable;
    
    $t = new Proc::ProcessTable( 'cache_ttys' => 1 );  
    
    foreach $p ( @{$t->table} ){
        if ($p->cmndline =~ /perl.*dispatch.fcgi/) {
            my $run_time_min = $p->time/(1000000*60);
            if ($run_time_min >= 15) {
                # print "Found this job to kill: ". $p->pid . $p->cmndline."\n". $run_time_min . "\n";
                kill 'KILL', $p->pid;
            }   
        }   
    }