Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Script for Killing Process of Long Running Time in Linux/Unix

by monkfan (Curate)
on Oct 25, 2007 at 12:24 UTC ( #647154=perlquestion: print w/ replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Dear experts,

Since some of the process spawned by particular user/pid takes too long time to run. I have this particular task I want to achieve with Perl:

  1. Get PID and running time info of a particular Username
  2. Check how long is the running time.
  3. Kill that process if it exceed a pre-defined threshold.

I intend to do a cron job for this Perl script.

Is there any simple way to do that with Perl? My main concern is No.1 and 2. Is there any CPAN module that get that information? For killing a process, maybe I can just use Perl's 'system' command.

Regards,
Edward

Comment on Script for Killing Process of Long Running Time in Linux/Unix
Re: Script for Killing Process of Long Running Time in Linux/Unix
by Corion (Pope) on Oct 25, 2007 at 12:30 UTC
Re: Script for Killing Process of Long Running Time in Linux/Unix
by weismat (Friar) on Oct 25, 2007 at 13:20 UTC
    Alternatively you may use Unix::PID.
Re: Script for Killing Process of Long Running Time in Linux/Unix
by Fletch (Chancellor) on Oct 25, 2007 at 13:41 UTC

    Just to throw this (possibly) related tidbit out: If you're interested in / worried about how much CPU time (as opposed to wall clock time) the process is using you might look at setting RLIMIT_CPU (either by your systems per-user login defaults, or your shell's ulimit command, or from Perl with BSD::Resource). Setting that sends a signal to the process when it has over stayed its welcome and run too long (on CPU doing actual work time).

Re: Script for Killing Process of Long Running Time in Linux/Unix
by tuxz0r (Pilgrim) on Oct 25, 2007 at 13:56 UTC
    Another possible solution, since you mentioned the system() call, is to make it a one liner,
    ps -lf | grep "user" | perl -ane '($h,$m,$s) = split /:/,$F[13]; kill + 9, $F[3] if ($h > 1);'

    You can change the criteria for killing the process, but in this case I pick any with a TIME greater than 1 hour.

    Though, this will be dependent on the particulars of the 'ps' command on your flavor of unix, most notably whether it's a BSD or SySV flavor. BSD has the -u option to select only those processes for a user, but the other does not which is why I typically just use grep "username".

    If your particular ps is different, you'll only have to change the indexes into @F. In the above case, the TIME is at index 13 in the format "00:00:00" and the PID of the process is index 3 and is just an integer.

      Thanks so much. That's just what we need.
      BTW do you know how can I put that one-liner in CRONTAB?
      $ crontab -e

      Regards,
      Edward
        You can put it in crontab directly (everything after the first five fields is considered the command),
        * * * * * ps -lf | grep "user" | perl -ane '($h,$m,$s) = split /:/,$F +[13]; kill 9, $F[3] if ($h > 1);'
        or, wrap it in a shell script and call the shell script from your crontab entry instead.
        #!/bin/sh # longprockill.sh ps -lf | grep "user" | perl -ane '($h,$m,$s) = split /:/,$F[13]; kill + 9, $F[3] if ($h > 1);'
        And call it crontab like so,
        * * * * * longprockill.sh

        ---
        echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://647154]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-10-22 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls