Re: At most 1 running copy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux