in reply to How to limit CPU utilization by ANY process with a perl script?

Not sure if this is what you want, but in Linux, you can do something like this...
ps aux | perl -lane 'print if $F[3] > 70'
This basically prints the CPU utilization if it is more than 70%. We can replace the 'print' with a force kill command if this is the answer to your question.
  • Comment on Re: How to limit CPU utilization by ANY process with a perl script?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to limit CPU utilization by ANY process with a perl script?
by Bloodnok (Vicar) on Feb 24, 2009 at 08:22 UTC
    ...or, rather than replacing print(1) with kill(1), why not replace with nice(1) instead - it is what it's there for , after all :-)

    A user level that continues to overstate my experience :-))

      However, in the case of an idle system, nice will not limit use to <= 70%. If the system is idle, it will still use as much as it needs to do what it is trying to do.

      This is an issue for the OS scheduler, or each program needs to give up the processor voluntarily if it goes over 70%.

      Update: or use something with SIGSTOP/SIGCONT - see tilly's post below.

      --MidLifeXis

Re^2: How to limit CPU utilization by ANY process with a perl script?
by Lawliet (Curate) on Feb 24, 2009 at 01:45 UTC

    Hmm, I think I might be misunderstanding what your one-liner is doing, but isn't the fourth element of @F %MEM, not %CPU?

    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND marc 9764 0.0 1.9 85544 40380 ? S 15:47 0:07 pidgin
    $ ps aux | perl -lane 'print if $F[3] == 1.9' marc 9764 0.0 1.9 85500 40376 ? S 15:47 0:07 pidgin

    And you didn't even know bears could type.

      That's true. In my case, the 4th element is for the CPU. I guess ps gives a different output depending on your Linux flavor.