Quick-and-dirty way to ensure only one instance of a shell script is running at a time

前端 未结 30 2989
忘掉有多难
忘掉有多难 2020-11-22 02:57

What\'s a quick-and-dirty way to make sure that only one instance of a shell script is running at a given time?

30条回答
  •  离开以前
    2020-11-22 03:07

    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.

提交回复
热议问题