What\'s a quick-and-dirty way to make sure that only one instance of a shell script is running at a given time?
You can use GNU Parallel
for this as it works as a mutex when called as sem
. So, in concrete terms, you can use:
sem --id SCRIPTSINGLETON yourScript
If you want a timeout too, use:
sem --id SCRIPTSINGLETON --semaphoretimeout -10 yourScript
Timeout of <0 means exit without running script if semaphore is not released within the timeout, timeout of >0 mean run the script anyway.
Note that you should give it a name (with --id
) else it defaults to the controlling terminal.
GNU Parallel
is a very simple install on most Linux/OSX/Unix platforms - it is just a Perl script.