Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am just wondering... we have a script that once in a while will hang, we have went through every line of the code, which has like 30k lines in all, it is a huge program. I had every loop of every type do a kill if it runs wild after like 100k loops, since there is nothing that should get that many loops...

Anyhow, I have tried every thing a user would do to make it hang myself and am not able to duplicate anything they do.

Once one hangs a bunch will start to hang, but even then I cannot get one to hang, so I am figuring it is a script or something that someone is running, maybe a spider attempting to index it or something. What I do in shell is run this command:

ps auxww | egrep [m]ember.cgi | awk '{print $2}' | while read PID; do +kill -9 $PID; done
the name of the file listed in 'top' under the 'command' section is the name of the file: member.cgi

So, that is what that shell command does, it gets all those commands and kills them.

I want to set that on a cron job that runs every so often to kill any of those processes. I ran it while I had 10 people load the website and since it is only a redirect script, not one of them was interrupted by it. So I am confident that it will not hurt any visitors.

What I did was create a script for root to run:
#!/usr/bin/perl print "Content-type: text/plain\n\n"; `ps auxww | egrep [m]ember.cgi | awk '{print $2}' | while read PID; do + kill -9 $PID; done`; print "done\n"; exit;
however, when I run it from shell:
perl root_cron.cgi
I get this error: "kill -n signum | -sigspec pid | jobspec ... or kill -l sigspec"

I only get that only if there are member.cgi scripts hung, if none are hung I only get the output: done

Is there a way with Perl to do this?

I am currently re-doing how the redirect script will work so I will not need this for long, I should have the whole thing re-programmed in the next 3 to 6 months, so I do need it for now.

I would really appreciate any thing you can point me to.

Thanks,

Richard

Replies are listed 'Best First'.
Re: unix command question and perl
by JavaFan (Canon) on Feb 13, 2011 at 15:37 UTC
    Many OSses have a command that does what you want without the pipes and the loops. killall (Linux, MacOS X) and pkill (Solaris, HP-UX) come to mind.
      Please don't get into the habit of using 'killall'. It does different things on different platforms and is *dangerous*.
        Don't go scare mongering. Obviously, the OP doesn't need to run his command on different platforms (or he wouldn't use ps with a bunch of options.... ;-)). killall on Linux or MacOS isn't more dangerous than his long pipe; nor is pkill on Solaris. Sure, killall on Solaris does what it says it does, but I didn't advocate that.

        Giving advice to "not get into the habit" of using X, because on some platforms X does something else than it does on other platforms is pointless scaremongering - and not really helpful. Being afraid makes louse programmers and sysadmins.

Re: unix command question and perl
by Anonymous Monk on Feb 13, 2011 at 08:58 UTC
    Nevermind, I worked it out... thanks.
      For the benefit of all, would you mind sharing how you worked it out? Thanks.

      "Its not how hard you work, its how much you get done."