2

Background information:

I have a Perl script converted to a EXE file, script.exe, that runs on Windows. Script.exe reads and will update at times to/from a configuration file, config.ini.

Script.exe is either executed with a command-line argument specifying location of the config.ini file. Or it can be run without a command-line argument and will default to the default location for the config.ini file.

Goal:

Prevent script.exe from being executed more than once using the same config.ini file.

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1279586
  • 257
  • 3
  • 16

1 Answers1

2

Use flock on the config file.

use Fcntl qw( LOCK_EX LOCK_NB );

open(my $config_fh, '<', $config_qfn)
   or die("Can't open config file \"$config_qfn\": $!\n");

if (!flock($config_fh, LOCK_EX | LOCK_NB)) {
   if ($!{EWOULDBLOCK}) {
      die("Config file $config_qfn already in use\n");
   }

   die("Error trying to lock the config file: $!\n");
}

... # Rest of program. Don't close $config_fh
ikegami
  • 367,544
  • 15
  • 269
  • 518