Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Killing Apache

by Juerd (Abbot)
on Dec 30, 2002 at 10:02 UTC ( [id://223012]=CUFP: print w/replies, xml ) Need Help??

On a new server that I'm installing, Apache often gets into some infinite loop, slurping all resources. I wanted to kill the Apache children with Apache::Resource, but that module does nothing at all on this system. So until I found a real solution for this problem, I quickly created this script to kill Apaches that use more than 16 MB of memory.

#!/usr/bin/perl -wl use strict; use File::Find::Rule qw(find); sub readfile { open my $fh, shift or return ''; local $/; return readline $fh; } my @files = find 'file', name => 'status', in => '/proc'; # 33 is the Apache user's uid @files = grep readfile($_) =~ /^Uid:\s*33\b/m, @files; @files = grep readfile($_) =~ /^VmData:\s*(\d+)/m && $1 > 16384, @file +s; /(\d+)/ and print("Killing $1\n") and kill 15, $1 for @files;
And in root's crontab:
* * * * * perl /root/apachekill.pl 2>/dev/null
And no, this program is not efficient at all. I should cache the readfile()s, and not use the temporary array. There probably is some F::F::R extension that looks into files that could eliminate my greps and readfiles completely. Doesn't matter much, I'll have to find a real solution anyway :)

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Killing Apache
by Aristotle (Chancellor) on Dec 30, 2002 at 16:47 UTC
    There probably is some F::F::R extension that looks into files that could eliminate my greps and readfiles completely.
    Yep, and it's waiting right there in the File::Find::Rule POD.. :) I know you said it's throwaway, but for the heck of it (untested):
    #!/usr/bin/perl -w use strict; use File::Find::Rule; my %uid; my %vm_data; print("Killing $_\n"), kill 15, $_ for map /(\d+)/, File::Find::Rule ->file ->name('status') ->grep(sub { my $fullname = $_[2]; $uid{$fullname}++ if /^Uid:\s*33\b/; $vm_data{$fullname}++ if /^VmData:\s*(\d+)/ and $1 > 16384 +; return $uid{$fullname} and $vm_data{$fullname}; }) ->in('/proc');
    What can I say? File::Find::Rule rocks. :)

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://223012]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found