Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am working on a simple perl module of subroutines for developers on a development team to use. The goal is to make a lot of different perl scripts written by several different people do a lot of things similarly. One of the things we want all of our scripts to do is write a pid file. So in my library I'm writing a subroutine to get the pid of the script and write a file named "<process name>.pid", which will contain the pid of the script. Getting the pid is easy enough I think (just call getppid), but what about getting the name of the script that's running? Is there a good way to do this other than parse the output of "ps ax|grep $pid"?

Replies are listed 'Best First'.
Re: Getting PID and Process Name
by Zaxo (Archbishop) on Sep 09, 2003 at 14:35 UTC

    In perlvar, see $0 and $$.

    After Compline,
    Zaxo

Re: Getting PID and Process Name
by broquaint (Abbot) on Sep 09, 2003 at 14:37 UTC
    You're after the variables $0 and $$:
    $0
    Contains the name of the program being executed. On some operating systems assigning to "$0" modifies the argument area that the ps program sees. This is more useful as a way of indicating the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.)
    $$
    The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across fork() calls. (Mnemonic: same as shells.)
    See. your local perlvar for further details.
    HTH

    _________
    broquaint

Re: Getting PID and Process Name
by jeffa (Bishop) on Sep 09, 2003 at 14:36 UTC
    Unless you are trying to find out the name of the script from another script, your script can do something like:
    open PID, '>', "$0.$$" or die "can't write pid file: $!";
    $$ is the current process id and $0 is the name of the script.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Getting PID and Process Name
by Abigail-II (Bishop) on Sep 09, 2003 at 15:17 UTC
    getppid() returns the process ID of the parent. As indicated by others, use $$ to get the process ID of the current process.

    Abigail

Re: Getting PID and Process Name
by blue_cowdawg (Monsignor) on Sep 09, 2003 at 14:45 UTC

    Greetings and hallucinations Anonymous One,
    Consdier the following code:

    #!/usr/bin/perl -w + use File::Basename; use strict; use warnings; use diagnostics; + my $pid=$$; + my $name=basename($0); + my $pidfile="/var/tmp/" . $name . "." . $pid; + printf "I'm going to use: %s\n",$pidfile;
    When this is run it yields:
    --$ perl procpid.pl I'm going to use: /var/tmp/procpid.pl.1974

    There is more than one way to do this but that's one way. Further to actually assert the "lock" you would add the following:

    use FileHandle; # Add this with the rest of the uses : : # do the filename calculation steps shown above : my $fh=new FileHandle("> $pidfile") or die $!; print $fh "$$"; # Or whatever you want to write undef $fh; #automatically closes the file

    Do you want to get rid of the ".pl" part of the name? Do this:

    $pidfile =~ s/\.pl//;

    Enjoy!


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Getting PID and Process Name
by welchavw (Pilgrim) on Sep 09, 2003 at 14:45 UTC

    You wrote...

    "The goal is to make a lot of different perl scripts written by several different people do a lot of things similarly." (emphasis added)

    Best of luck! (tough firmly in cheek)

Re: Getting PID and Process Name
by techy (Scribe) on Sep 10, 2003 at 01:44 UTC
    I'm not sure about your particular case, but you may want to take a look at Proc::PID::File for a general module from CPAN to manage pid files (it handles creating, verifying, and destroying PID files.)

    Regards,

    techy