in reply to Getting PID and Process Name
Greetings and hallucinations Anonymous One,
Consdier the following code:
When this is run it yields:#!/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;
--$ 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. | |
|
|---|