At most 1 running copy

John Summerfield debian at herakles.homelinux.org
Fri Nov 26 23:41:46 UTC 2004


On Friday 26 November 2004 22:44, Ed Wilts wrote:
> Yesterday 22:44:50
>
>
> I'm looking for a fool-proof way to ensure that at most 1 copy per user
> is running of a single Perl script.  This code is horribly ugly and
> needs to be scheduled frequently via cron but occasionally may run for a
> very long time.  I'd like to ensure that if a copy is already running,
> another one doesn't start up.  I need to do this with very minimal
> changes to the running code (as I said, it's ugly and already does a
> very poor job of error handling).
>
> Any ideas would be greatly appreciated.  The developer had this coded in
> there, but it's not always working correctly:
>
> my $My_Name = getlogin || (getpwuid($<))[0];
> my $ProgName = 'ftphandler.pl';  # update if program name changes
> # check to see if user is already running program
> my $Running=`ps -ef|grep $My_Name | grep perl | grep -c $ProgName`;
> chop($Running);
> if ($Running > 2) {
> #print "already running.\n";
> unlink("$Tmplog");
> exit;
> }
>
> It's not my code, but I get to be the lucky guy to fix it and I'm a
> crappy Perl coder...


Potential race condition there. Junk it.

I suggest you create  /var/run/lock-$USER-APPLICATION and lock it.

Once you get to lock it, you're the only one running.

Release the lock when you're finished. I don't see that it matters if you 
leave the file there, and I'd want to test it before telling you it's alwasy 
safe to remove the file. If you can remove it the unlock it that woild be 
okay, but that flies in the face of my experience in oter environments.





-- 
Cheers
John




More information about the fedora-list mailing list