#! perl -slw
use strict;
use Win32::Semaphore;
( my $semName = $0 ) =~s[\\][/]g;
die 'Program already running' if Win32::Semaphore->open( $semName );
my $sem = Win32::Semaphore->new( 1, 1, $semName ) or die $^E;
sleep 1000;
__END__
c:\test>start /b loner.pl
c:\test>loner.pl
Program already running at c:\test\loner.pl line 6.
I've used the script's full pathname as the semephore name, with backslashes switched to forward slashes as the former are disallowed. If there is the possibility of multiply named copies of the script, then use a well-known name instead.
Oh. And don't let $sem or whatever you call yours go out of scope otherwise the system semaphore gets freed. Obvious I guess, but it hung me up for a while.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|