From: Paul Archer Date: 2009-07-01T01:18:43+09:00 Subject: Re: locking a process 11:39am, Daniel DeLorme wrote: > Paul Archer wrote: >> I am in the process of developing a few scripts that: >> 1) pull images off a CF card on my laptop >> 2) move the images to a server >> 3) rename the images on the server according to the EXIF info >> >> >> I figure that for 2 & 3, the easiest way to make sure they work is to have >> them run in a cron job once a minute (or five). (Wake up, check to see if >> there's any work, either do it or quit.) >> >> The problem is that I need to make sure that if the script starts while >> another copy of the script is running, it quits. I can work out how to >> create lock files, stick in PIDs to make sure the script is really there, >> etc. But it seems like someone must have tackled this problem already >> (IIRC, I ran across a Perl module that back in my misspent youth). > > What I do for this is simply rename the process: > > abort("already running") if `pgrep -f my-wonderful-process` != "" > $0 = "my-wonderful-process" > Cool, thanks. I like this. Of course, it's still possible to have a race condition, but it should be pretty unlikely in my case. Paul