Category: | Utility Scripts |
Author/Contact Info | tye |
Description: | Run code in the background even more nicely than is possible with 'nice'. We have administrative tasks that run on the PerlMonks database server regularly. These make the site quite sluggish even if run via "nice -99". So I wrote this. It runs a command nice'd but every few seconds it suspends the command for several seconds. Updated. |
#!/usr/bin/perl -w use strict; use POSIX "WNOHANG"; Main( @ARGV ); exit( 0 ); sub Spawn { my $pid= fork(); die "Can't fork: $!\n" if ! defined $pid; return $pid if $pid; exec( @_ ); die "Can't exec $_[0]: $!\n"; } sub Sleep { my $secs= shift( @_ ); select( undef, undef, undef, $secs ); } sub Main { die qq[Usage: $0 [work wait [nice]] command [args] work Number of seconds to let "command" work at a time [3] wait Number of seconds to make "command" wait inbetween [6] nice Optional command like "nice" or [nice -20]\n] unless @_; unshift @_, qw( 3 6 nice -20 ) unless $_[0] =~ /^\d*\.?\d+$/; my $work= shift( @_ ); my $wait= shift( @_ ); my $kid= Spawn( @_ ); while( $kid != waitpid( $kid, WNOHANG() ) ) { Sleep( $work ); kill 'STOP', $kid; Sleep( $wait ); kill 'CONT', $kid; } exit( $? >> 8 ); } |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: breathe - beyond 'nice'
by dragonchild (Archbishop) on Feb 18, 2004 at 01:52 UTC | |
by tye (Sage) on Feb 18, 2004 at 04:07 UTC |