#!/usr/bin/perl use strict; use Fcntl qw( :flock ); use Getopt::Long; GetOptions( 'test' => \my $option_test ); print "Running $0\n"; check_process() if ( ! defined( $option_test ) ); sleep 30; print "Running $0\n"; sub check_process { open( SELF, '<', $0 ) || die "Error: Cannot open $0 for locking - $!\n"; flock( SELF, LOCK_EX | LOCK_NB ) || die "Error: Another $0 is already running.\n"; return 1; } #### $ perl test.pl --> should run if there is no test.pl running. $ perl test.pl ---> should abort if there is a test.pl running already. $ perl test.pl --test --> should run even if test.pl is already running. $ perl test.pl --test --> should abort if there is a (test.pl --test) running as well.