Sunday, 19 July 2009

run only one instance of perl script

This is one out of many ways of allowing only one instance or perl script to run using cron..

# Needed for "Only allow one process of this script" rule
use Fcntl ':flock';
# Only allow one process of this script
INIT {

# Lock current script's disk file so that it can only be accessed by the current running script instance.
# When the script exits or dies, the lock is released.
open LH, $0
or die "Can't open $0 for locking!\nError: $!\n";

# LOCK_EX means exclusive lock, LOCK_NB means non-blocking
flock LH, LOCK_EX | LOCK_NB
or die "$0 is already running somewhere!\n";
}

No comments:

Post a Comment

Tweets by @sriramperumalla