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

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

    I use oneliner @ the very beginning of script:

    #!/bin/bash
    
    if [[ $(pgrep -afc "$(basename "$0")") -gt "1" ]]; then echo "Another instance of "$0" has already been started!" && exit; fi
    .
    the_beginning_of_actual_script
    

    It is good to see the presence of process in the memory (no matter what the status of process is); but it does the job for me.

提交回复
热议问题