in reply to Make a perl script self aware...

Fellow Monks,

the following is the solution I choose the problem described above.

I did use demerphq's code, removed the indirect filehandles and commented it for maintenance reasons.

BTW: demerphq, is there a reason for the use of indirect filehandles? The script works without them, and it's a lot easier to maintain without them.

As demerphq noted before... the DIE is mute on Windows most of the time.

Final Code:

# Needed for "Only allow one process of this script"-rule use Fcntl ':flock'; INIT { open LH, $0 or die "Can't open $0 for locking!\nError: $!\n"; # lock file so that it can only be accessed # by the current running script # DIE is mute (doesn't print to the screen) on Windows flock LH, LOCK_EX|LOCK_NB or die "$0 is already running somewhere!\n"; }
Thanks for all your help, you are the monks that make this a great community.

/neuroball/

Replies are listed 'Best First'.
Make a perl script self aware... all platforms
by monkeyperl (Initiate) on Jun 25, 2008 at 00:51 UTC
    This is an interesting problem indeed.
    I have used this method of opening the script itself and placing an exclusive lock on it for quite sometime, without problems

    However, I have started working with Unix AIX recently and the solution no longer works :(

    I had never had any problems with several Linux distros, FreeBSD, Mac OS X or Windows.

    But sadly under AIX the script fails to get a lock on itself, weather it is running or not.

    I added a $! to the flock like so:
    flock LH, LOCK_EX|LOCK_NB or die "$0 is already running somewhere!\n$!";

    and the OS error message is:
    A file descriptor does not refer to an open file

    Interesting, the error message reads as if the file open failed,
    and yet it was fine as far as I can tell because the 'or die' for open did not get invoked... odd.

    Any suggestions?