Some remarks
- Your lockfile's path will be relative to your current working directory.
I'd suggest using an absolute path to avoid miscommunications.
- Your sleep is called without argument, which means it'll sleep forever.
- Are you sure that a parallel instance should exit? Without
LOCK_NB it would wait till the lock is free.
- You are not using parens for your arguments for flock(), precedence issues with | would make me nervous here. I'd need to look it up...
it should work in this case tho.
Regarding debugging
You shouldn't rely on ps for debugging, append with >> to (another) absolute_path/logfile when your script is
- started,
- exiting,
- working,
- stopped,
including process ID and timestamp.
Please come back if you can really reproduce a problem.
Maybe of help:
File lock demo