On Wed, 9 Dec 2009 11:43:44 -0800 (PST)
cerr <ron.egg...@gmail.com> wrote:

> Hi There,
> 
> I use below code to make sure i have only one instance of my script
> running at a time. But weirdly enough i sometimes seem to have running
> two instances. This script is getting called on a regular basis by a
> cron job and may take a long time (30+min)to complete but still...I
> can't figure out what would be wrong with this code, anyone?
> 
> #[PID]
>   #Check if PID file already exists
>   if ( -s $PIDloc){
>     print "THERE IS ALREADY AN INSTANCE OF UPDATESERVER RUNNING\n
> check \"".$PIDloc."\"\n";
>     syslog('info',"THERE IS ALREADY AN INSTANCE OF UPDATESERVER
> RUNNING \n check \"".$PIDloc."\"");
>     exit(999);
>   }
>   #if it doesn't exist, create it (gets ereased atr the bottom of MAIN
> on completion)
>   my $pid = $$;
>   open my $fd,'>', $PIDloc or die $!;
>   print $fd $pid;
>   close $fd or die $!;
> #[/PID]





Sorry I'm a bit late on this, but I have successfully used this snippet
to check for a process, starts it if it doesn't exist, kills it if it
does.

=============================================
 #!/usr/bin/perl -w

 use strict;

 my $program = "myprogram";
 my $status  = `/bin/ps cat | /bin/grep $program`;

 if ( length($status) > 0 ) {

 $status =~ /(^\d+)/;
    print "$1\n";                         #extract pid from here
 exec "kill -9 $1"
 }
     else { exec "/path/to/myprogram" }    # start program

=============================================

You maybe able to adapt it for your needs. EG, delete the PID file if
the program is running.



Owen

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to