use Fcntl qw(O_WRONLY O_CREAT O_EXCL); my $lockfile = '/tmp/lock'; if (sysopen(FH, $lockfile, O_WRONLY | O_CREAT | O_EXCL)) { # sysopen will only work if lockfile does not exist # do stuff close FH; unlink $lockfile or die "Can't unlink lockfile $lockfile $!"; } else { die "$lockfile exists so another instance of the script is running!"; exit; }