in reply to how to exit if tool is already running

The easiest is to check for the existence of a special file on a start-up if the file not exists create a temporary file and delete this file after the script has finished. Then you can decide to exit the script if the file already exists. This would be a problem if the first started script crashes without deleting a file. Maybe you want to do a double check if the file exists and the process perl.exe is running. This technique is pretty common for *nix utilities to prevent double execution as it is easy to implement but you should be aware this is not very reliable.

UPDATE: maybe you want to read about File locking and implement this in your script as you mentioned the problem comes from parallel access to the same file.

  • Comment on Re: how to exit if tool is already running

Replies are listed 'Best First'.
Re^2: how to exit if tool is already running
by JavaFan (Canon) on Aug 12, 2010 at 16:31 UTC
    The easiest is to check for the existence of a special file on a start-up if the file not exists create a temporary file and delete this file after the script has finished.
    That's not easy at all. In fact, if you do it as you state, you have a solution with a race condition, as you aren't doing the "check if a file exists; create it otherwise" atomically. Furthermore, you have a problem if the program dies and doesn't get the chance to clean up the special file. Or if something else removes the file.
      JavaFan, I know we have had this discussion before, but the question says:
      I have written a small Tool in perl. ... Sometimes accidentally we clicked twice the tool. (emphasis mine)
      Given this, the complaint about an atomic procedure seems somewhat pedantic for a small (probably Q&D) tool.
      Furthermore, you have a problem if the program dies and doesn't get the chance to clean up the special file.
      Can be handled by either an exit handler or checking to see if the program is already running.
      Or if something else removes the file.
      Write and/or exclusive locks handle this.

      Even if it is not how you would do it (possibly for a full blown application), it may well fit the bill for this task.

        Furthermore, you have a problem if the program dies and doesn't get the chance to clean up the special file.
        Can be handled by either an exit handler
        But they aren't always called, are they? The OS may crash. Something could kill it with SIGKILL. The application may exec.
        checking to see if the program is already running.

        Write and/or exclusive locks handle this.

        So, now we're down to using external files, exit handlers, checking the process table, and using locks (which doesn't prevent a rm of the file).
        Even if it is not how you would do it (possibly for a full blown application), it may well fit the bill for this task.
        Or it may not. It sure ain't any simpler than
        use Fcntl ':flock'; flock DATA, LOCK_EX|LOCK_UN or exit;
        which doesn't require exit handlers, external files, does the right thing when the program exits unexpectedly, and doesn't have to check to process table.

        Now, I'm not claiming it isn't possible. I'm disagreeing with you that it's the easiest solution. Self-locking is easier than external locking.