use Mutex; my $mutex = Mutex->new( path => $0 ); # terminate script if a previous instance is still running exit unless $mutex->timedwait( 2 ); ...