Getting the process id for current perl script

Solution 1:

You can use $$ to get the process ID of the perl interpreter running your script:

iancs-imac:Documents ian$ cat test.pl 
print "$$\n";
sleep(10000);
exit()

ians-imac:Documents ian$ perl test.pl 
42291

In another shell:

iancs-imac:~ ian$ sudo ps -ef | grep perl
  501 42291 42281   0   0:00.00 ttys000    0:00.01 perl test.pl
  501 42297 42280   0   0:00.00 ttys001    0:00.00 grep perl

To learn more about about special Perl variables:

perldoc perlvar

Or see the official online version of that information.

Solution 2:

In addition to $$ as Ian mentions, I'm a fan of making code more readable.

To that end, Perl supports the mnemonic $PID if you use English to enable the aliases.