#!/usr/bin/perl -l use warnings; use strict; use Socket; my $sock_fh; BEGIN { my $port = 987654; # any constant specific to this app socket($sock_fh, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "socket: $!"; bind($sock_fh, sockaddr_in($port, INADDR_LOOPBACK)) or (warn "another process is already running ($!)\n" and exit(1)); } # BEGIN: where you do your work print "send interupt to kill process"; while (<>) { sleep 1; } # END: where you do your work END { $sock_fh->close() if defined $sock_fh; }