I have a code in which below line is used due to which i got stuck.I have hunted the web but did not understand well.just got to know the basic set of info as below

lockfile can be used to create one or more semaphore files. If lockfile can’t create all the specified files (in the specified order), it waits sleeptime (defaults to 8) seconds and retries the last file that didn’t succeed.

Can any one please let me know the understanding. Thanks in advance NOTE: LockFile is a path variable

if lockfile -1 -r0 ${LockFile}

The point of lockfile is to protect the execution of some commands with a lock (also called mutex). That is, consider the following program:

if lockfile "$LockFile"; then
  more stuff
  rm -f "$LockFile"
  echo 1>&2 "Fatal error: another process has been keeping the lock for too long or the lock file is inaccessible"
  exit 3

Only a single instance of the script at a time can be executing the stuff; more stuff part. If you start a second instance, then the second will wait until the first has finished (more precisely, until it's deleted the lock file), up to a certain maximum time given by the -TIME option.

With the parameter -r0, the second instance doesn't wait at all: it immediately goes to the else branch. So you can't run concurrent instance of this script (or this part of the script, if this is just one part of a bigger script).

| improve this answer | |
  • What about -1 option!! – api1411 Jun 9 '16 at 6:25
  • @api1411 1 is a number. It's the time to wait, except that it isn't used because -r0 says not to wait. – Gilles 'SO- stop being evil' Jun 9 '16 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.