use Fcntl qw (:DEFAULT :flock :seek); my $file = '/dev/shm/tmpcount.txt'; my $c; do { if (-e $file) { $c = &fileread($file); die "C is empty" unless ($c); print "count is $c \n"; } else { print "File not found\n"; } } until ($c == 180); sub fileread { # Read using perl IO my $file = shift; open(my $fh, "<", $file) || die "Can't open $file for reading: $!"; my $mode = LOCK_SH; flock($fh,$mode) or die "couldn't lock"; my $string = do { local $/; <$fh> }; close $fh; return $string; }