...of a shell script for rsync that won't start again if it is already running? I thought of using a lock file, but what if it is killed mid script or bombs? -- MailScanner is like deodorant... You hope everybody uses it, and you notice quickly if they don't!!!!
Actually if you use a language that supports flock() do the following: Create lock file at installation of the script. In script call flock(2) in a non-blocking manner against this file. If you don't aquire the lock then exit with suitable message. If you do get the lock do your stuff and exit. Locks aquired by flock automatically go away when the file handle is closed (and the filehandle automatically gets closed like or not after you exit a proccess...also in various languages scoping will also apply). This is easily done in perl (man perlfunc and look up flock()), and if your comfortable in C its pretty trivial create said wrapper. The key is the file must pre-exist before your script is ever called, because there is a race condition on creating the file, but you guarantee the files pre-existance the race condition is removed. Typically I would make the "lock" file owned by the package that delivers the software. No fuss, no muss. Good luck...james On 8/29/06, Scott Silva <ssilva at sgvwater.com> wrote:> ...of a shell script for rsync that won't start again if it is already running? > I thought of using a lock file, but what if it is killed mid script or bombs? > > -- > > MailScanner is like deodorant... > You hope everybody uses it, and > you notice quickly if they don't!!!! > > _______________________________________________ > CentOS mailing list > CentOS at centos.org > http://lists.centos.org/mailman/listinfo/centos >
Scott Silva wrote:> ...of a shell script for rsync that won't start again if it is already running? > I thought of using a lock file, but what if it is killed mid script or bombs?You write your PID to the lock file, then in startup, you check if the lock file exist and if the PID process is running, then it is still active. It could be that the process is just hung, so this method will only help assure that you do not start a second copy. -- //Morten Torstensen //Email: morten at mortent.org //IM: Cartoon at jabber.no morten.torstensen at gmail.com And if it turns out that there is a God, I don't believe that he is evil. The worst that can be said is that he's an underachiever.
On Tue, Aug 29, 2006 at 11:13:43AM -0700, Scott Silva wrote:> ...of a shell script for rsync that won't start again if it is already > running? I thought of using a lock file, but what if it is killed mid > script or bombs?1. Use a directory for locking instead of a file, because testing for existence and creating the directory is an atomic operation. 2. Use trap to remove the lockdir so basically: if ! mkdir $LOCKDIR ; then echo "Lockdir exists; aborting." exit 1 fi trap "rmdir $LOCKDIR; echo 'mirror script exiting'" EXIT TERM INT QUIT rsync $RSYNCOPTS $REMOTE $LOCAL -- Matthew Miller mattdm at mattdm.org <http://mattdm.org/> Boston University Linux ------> <http://linux.bu.edu/>