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

前端 未结 30 3188
忘掉有多难
忘掉有多难 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:22

    Some unixes have lockfile which is very similar to the already mentioned flock.

    From the manpage:

    lockfile can be used to create one or more semaphore files. If lock- file 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. You can specify the number of retries to do until failure is returned. If the number of retries is -1 (default, i.e., -r-1) lockfile will retry forever.

提交回复
热议问题