[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Quick Bail




On Sep 1, 2005, at 5:22 PM, Kenneth Thompson wrote:
A short one-liner to keep a script from executing more than one at a time (say a long running script kicked off from cron every minute that runs more than a minute occasionally...
 
my @ps = `ps -ef | grep $0 | grep -v grep`; exit if ( @ps > 1);

I like it! Quick and dirty! -grin-

and a more advanced version...
 
my @procs = grep /perl .*invan\.pl/, `ps axw`; 
if (@procs > 1) { 
   unless ($ENV{USER} eq "jhannah") {  # Hopefully that dork knows what he's doing, so let him do whatever.        die "Looks like I'm already running. I refuse to run on top of myself.\n @procs";
   }
} 

-laugh- Not sure I'd call my hack there more advanced. But, it is handy in that it throws a printed list of the procs that were already running, and lets me, specifically, shoot myself in the foot when I want to. -grin-

For serious applications I've used Proc::ProcessTable before:

   http://search.cpan.org/~durist/Proc-ProcessTable-0.40/ProcessTable.pm

I assume it's more portable than `ps -ef`, and is great when you care about PIDs, PPIDs, memory utilization, etc.

Looks like there's tons of process control stuff on CPAN:

   http://search.cpan.org/search?query=proc&mode=all

Grin,

j