On Sun, Apr 27, 2008 at 2:49 AM, Michael Barnes <[EMAIL PROTECTED]> wrote:
> My apologies.  I know this is a cross-platform group, and that is fine.  
> However, if you are unable to understand simple Linux terminology, then it is 
> doubtful that you can help answer my question.  If the description is 
> nonsense to you, then please don't waste your or my time in an apparent slam 
> on my choice of operating systems.  If your reply represents the general 
> attitude of this list, then it looks like I've come to the wrong place for 
> assistance.
>
>  But, to simplify my request, a pid is a process id.  In Linux, the command 
> 'ps' will give a listing of process IDs.
>  I need the script to determine its own pid, then write that to a file.
snip

The variable $$ holds the PID for the current program*.  Typically a
pidfile is created in /var/run by saying something like

use strict;
use warnings;
use File::Basename;

BEGIN {
    our $program = basename $0;
    die "$program already running\n" if -f "/var/run/$program";
    open my $pidfile, ">", "/var/run/$program"
        or die "could not open /var/run$program: $!";
    print $pidfile "$$\n";
    close $pidfile;
}

#make sure the last thing done is the removal of the pidfile
#note: this happens even if we die
END {
    our $program;
    unlink("/var/run/$program")
        or die "could not delete /var/run/$program";
}

#the rest of your code

There are also a few modules on CPAN that automate this to a certain
extent for you: Proc::Pidfile**, File::Pid***, and
File::Pid::Quick****.

* see perldoc perlvar or
http://perldoc.perl.org/perlvar.html#$PROCESS_ID for more information
** http://search.cpan.org/dist/Proc-Pidfile/Pidfile.pm
*** http://search.cpan.org/dist/File-Pid/lib/File/Pid.pm
**** http://search.cpan.org/dist/File-Pid-Quick/Quick.pm
-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to