Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating and using a Lockfile 1

Status
Not open for further replies.

netman4u

Technical User
Joined
Mar 16, 2005
Messages
176
Location
US
Hello all,

I am trying to create a lockfile in my program to ensure only one instance is running at a time. I'm having some troubles getting my logic working. Here is my code:

Code:
#!c:\\perl\\bin\\perl 
use strict;
use warnings;
my $lock_file = "D:\\Stage\\lockfile";
if (-e $lock_file) {
	print "Found lock file $lock_file.
        exit 1;
}
open (LOCK, "+>$lock_file")or die ("Error opening $lock_file $1");;
print LOCK "print some junk\n";
close LOCK;
print "$lock_file\n";
if (unlink($lock_file) == 0) {
    print "File deleted successfully.";
} else {
    print "File was not deleted.";
}

The output I get EVRYTIME I run the program is:

Code:
D:\Stage\lockfile
File was not deleted.

It seems so simple I want to create the lockfile and if the program completes ok I want to delete it. But the file is never created and so never deleted!

If at first you don't succeed, don't try skydiving.
 
zero means the function failed, try using one to test for success:

Code:
if (unlink($lock_file) == [b]1[/b]) {
    print "File deleted successfully.";
} else {
    print "File was not deleted.";
}
 
Haaa! So it was being created and deleted it just happened so fast I could not see it! Thanks Kevin.

I got that right of a web tutorial to!


Whats the world comming to when you can't trust what you see on the web!

If at first you don't succeed, don't try skydiving.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top