分类: LINUX
2013-05-24 17:29:58
#### create myself pid file
my $pid = $$;
my $myname=$0;
print "myname: $myname\n";
my $pidFile = "/export/web/tmp/handle_report.pid";
#### check other myself,if exists then exit else write myself pid to pidfile
if ( -s "$pidFile") {
open PIDF, $pidFile || die "Can't open file $pidFile $!\n";
my $oldPid;
while (
$oldPid=$1 if ($_ =~ /^(\d+)/);
}
close(PIDF);
if($oldPid) {
#my $is_exist = qx/ps x -o 'pid' |grep -w "$oldPid"/;
#if($is_exist)
if(-d /proc/$oldPid )
{
print "Warn: already exist other myself pid=$oldPid,exit or kill $oldPid,check pidfile $pidFile\n";
exit;
}
}
}
#### other check method
my @psname = qx#ps x -o 'pid,cmd' | grep -w "$myname" | grep -Ev 'grep|/bin/sh'#;
if(@psname >1) {
print "Warn: ps name $myname already exist other myself exit\n";
exit;
}
my $insertPID=qx/echo $pid >$pidFile/;