At most 1 running copy

Doug Stewart dstewart at atl.lmco.com
Sat Nov 27 02:34:11 UTC 2004


Ed Wilts wrote:

>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...
>
>Thanks,
>        .../Ed
>  
>
It might be a better idea to simply write out a "lockfile" and check for 
its presence before running.

-- 
------------
Doug Stewart
Systems Administrator/Web Applications Developer
Lockheed Martin Advanced Technology Labs 
dstewart at atl.lmco.com




More information about the fedora-list mailing list