4 ms·
Great article, but a common scenario is missing: I want to run a long running script, but not if another copy of it is already running.
by mooneater 7y ago
Great article, but a common scenario is missing:
I want to run a long running script, but not if another copy of it is already running.
- dymk 7y agohttps://linux.die.net/man/1/flock https://linux.die.net/man/1/flock would probably help
- fwip 7y agoA word of caution on flock: it may fail when used on some network filesystems, and it is tricky to make sure you've got it right. If you're locking on-disk it's pretty straight-forward, though.
- sirn 7y agoflock(1) is not POSIX, though. mkdir(1) can be used if you absolutely want a POSIX way to manage locks. For example: if ! mkdir .lock; then printf >&2 "Already running?\\n" exit 1 fi Some network file system implementation do not guarantee atomic mkdir, so you still need an extra caution with this method.
- posix_me_less 7y agoExistence of a file is an unreliable indicator of script instance running. Much more reliable is to search the script name or other characteristic in the list of running processes. To use this, the script has to have unique name though.