#!/usr/local/bin/perl use strict; use warnings; $| = 1; use Fcntl qw( :flock); my $name = 'ttt'; # process name my $pid_file = "/var/run/$name.pid"; open my $pid_handle, '+<', $pid_file or die "Can't access '$pid_file': $!"; flock $pid_handle, LOCK_EX | LOCK_NB or die "Process $name is still active"; truncate $pid_handle, 0 or die "Truncating '$pid_file' failed: $!"; print $pid_handle "$$\n"; # Normal processing starts here # # ... sleep 10; # for testing