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

I have a heavy resource hog maintenance type script.
I want it to run when the computer's resources have been free form more then x time.
I can think of some strange ways to go about this.

Maybe this could even be a sort of uh.. "downtime" cron- system where you enter commands to be executed not at certain moments- but when the resources have been free for more then x time.

Maybe halt when user comes back- Or some other service is being requested. sort of like a... screensaver??? Maybe a screensaver which runs commands?? That would do no good on a shell thou.. hmm.
Assign them priority etc.

Is anybody using something like this?

I'm on 2.6 linux kernel.

  • Comment on (OT) Run task only when resources have been free for x time

Replies are listed 'Best First'.
Re: (OT) Run task only when resources have been free for x time
by Corion (Patriarch) on Nov 12, 2008 at 17:07 UTC

    See Chronic, which does just that. Also consider using nice and ionice.

      IIRC you can do this-

      use POSIX; POSIX::nice(40);

      -to get a better behaved script. Some systems top off at 19, again IIRC, but 40 should max whatever nice is available.

Re: (OT) Run task only when resources have been free for x time
by Illuminatus (Curate) on Nov 12, 2008 at 14:47 UTC
    Why would you not have the script run all the time, periodically checking the resources in question, and running whenever you feel resources are 'free' enough. For Linux, you can use Sys::Load to get load average. You can use the Linux free command to evaluate available memory.
      free is cool- but.. it just shows you memory usage. There's a ton of stuff that uses little memory but will flirt excessively with the cpu.
        I mentioned free because you did not indicate which resources were important to you. If cpu is the overriding factor, then Sys::Load is probably your best bet. Load average is the best generalized mechanism for gauging CPU utilization. When this number exceeds 1, it means there are more threads/processes waiting to run than are able to. On a single CPU system, if all 3 average values are greater than 1, then the system is experiencing sustained load.

        It might be enough to see all 3 load values be less than 1. Of course, this is all backward-looking. It is a little trickier to start your 'hog' process, and then have something more important decide it needs to run.