Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I get the process id for the perl process that's running the current script? getppid() doesn't return the same pid as ps -ea| grep . Is there is an easy way or do I just run the ps -ea command within my script and trim off the other pieces of info?

share|improve this question
Should probably be migrated to – Ian C. Oct 17 '10 at 23:43
up vote 6 down vote accepted

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

iancs-imac:Documents ian$ cat 
print "$$\n";

ians-imac:Documents ian$ perl 

In another shell:

iancs-imac:~ ian$ sudo ps -ef | grep perl
  501 42291 42281   0   0:00.00 ttys000    0:00.01 perl
  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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.