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

I am running this as a daemon, but it's hammering my CPU. I tried BSD::Resource, but I'm sorry to say, I can't figure out how to make it work. I'm a newby on a deadline and I need code help please!Thanks, great ones!
#!/usr/bin/perl use Proc::Daemon; Proc::Daemon::Init; use File::Copy; @dirs=qw(/mail/act1/ /mail/act2/ /mail/act3/ /mail/act4/); while(1){ $dir="/mail/main/"; opendir(MAIN,$dir) or die "Can't open $dir:$!"; while(defined($file = readdir MAIN)){ next if $file =~/^\.\.?$/; $path=$dir.$file; $newdir=@dirs[$i]; move ($path,"$newdir$file") or die "move failed:$!"; $i++; undef $i if ($i>$#dirs); } }

Replies are listed 'Best First'.
Re: Limiting CPU usage on this program...
by Abigail-II (Bishop) on Jun 04, 2002 at 16:51 UTC
    Well, your program keeps on going and going due to that while (1) loop. Of course it will "hammer" the CPU, if you are unlucky, it will hammer your disks as well.

    I'm not sure what you are trying to do (yeah, moving the files round robin to another directory - just like the question in some other thread). Why does it run as a daemon? What about cron? If you have an infinite loop, shouldn't it use a sleep, or wait for something to happen before continuing?

    Abigail

Re: Limiting CPU usage on this program...
by particle (Vicar) on Jun 04, 2002 at 17:22 UTC
    while(1){ # your code here # more code... sleep 300; # <--- without this line, you'll keep your disks busy, fo +r sure }
    i really hope this helps.

    ~Particle *accelerates*

Re: Limiting CPU usage on this program...
by vladb (Vicar) on Jun 04, 2002 at 17:43 UTC
    Also, you could make your script nice like this:
    # Set low priority (nice value) for this process... setpriority( "PRIO_PROCESS", 0, 10 ); while(1) { # some code # some more code ... sleep(300); # it's good to take a nap in the middle of a hard work! }
    The nicier a script, the less greedy it's going to be on system resources. And if it sleeps every once in a while on top of this, it's even more nicier ;)

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
Re: Limiting CPU usage on this program...
by airblaine (Acolyte) on Jun 04, 2002 at 18:14 UTC
    Thanks everybody for putting up with the rookie questions..it was the sleep(300); that finished it..(duh..I know) sorry for wasting some of your time...
Re: Limiting CPU usage on this program...
by ehdonhon (Curate) on Jun 04, 2002 at 19:10 UTC

    Like everybody already said, adding a sleep command is the best solution. Another solution that you might consider in addition to the sleep would be something like this:

    system( "renice 10 $$" );

    Of course, this is assuming that your OS has a renice command. And, you'd probably want to specify the full path to wherever it is located.