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

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

    Really quick and really dirty? This one-liner on the top of your script will work:

    [[ $(pgrep -c "`basename \"$0\"`") -gt 1 ]] && exit
    

    Of course, just make sure that your script name is unique. :)

提交回复
热议问题