0

I have used below code to check single instance of replace.exe is running. When i create replace.exe and run on windows, one more file with name replace(without any extension) of 0 kb size is getting created.

I don't want that file to be created else i want to delete the file automatically after execution of replace.exe is over.

Please help me.thanks

use Fcntl qw(:flock);
# Check if any instance of this script is already running
my $lock = "replace";
sub LockOut ( ) {
  &print_log ("A instance of this script is running. Therefore exiting. Please try after some time.");
  print "A instance of this script is running. Therefore exiting. Please try after some time.";
  exit 1;
}

open ( my $pid, '>', $lock );
flock ( $pid, LOCK_EX | LOCK_NB ) or LockOut( );
amon
  • 57,091
  • 2
  • 89
  • 149
  • use Fcntl qw(:flock); # Check if any instance of this script is already running my $lock = "replace"; sub LockOut ( ) { &print_log ("A instance of this script is running. Therefore exiting. Please try after some time."); print "A instance of this script is running. Therefore exiting. Please try after some time."; exit 1; } open ( my $pid, '>', $lock ); flock ( $pid, LOCK_EX | LOCK_NB ) or LockOut( ); – yogesh Yadav Mar 21 '13 at 12:22
  • See [Lock::File](http://search.cpan.org/perldoc/Lock::File). – Sinan Ünür Mar 21 '13 at 17:18

1 Answers1

0

I assume you are working with a perl script that is somehow compiled into a windows executable and you want it to be running in a single instance.

You could try the following:

use Fcntl qw(LOCK_EX LOCK_NB LOCK_UN);
if (!flock DATA,LOCK_EX|LOCK_NB)
{
    print STDERR "Already running! Exiting...\n";
    exit;
}

#### DO NOT REMOVE ####
__DATA__
This DATA-Block will be locked by the single-instance logic,
preventing it to be locked more than once.

In a perlscript, __DATA__ Blocks can be exclusively locked, but I don't know if your script-compiler preserves that feature.

If that somehow does not work, you can figure out the filename of the executable and lock that one instead, it should do the same thing as locking a file called 'replace'.