Shell script防止重复启动

如何保证shell脚本在系统里同一时间点,不会被重复执行。只需要在shell脚本头部追加以下代码。

_process=`basename $0`

_pcnt=`pgrep -fo ${_process} | wc -l`

if [ ${_pcnt} -gt 1 ]; then
  echo "This script has been running now. proc : ${_pcnt}"
  exit 1
fi

Leave a Reply