防止shell脚本被重复执行

 1 #!/bin/bash
 2 LOCK_NAME="/tmp/my.lock"
 3 if ( set -o noclobber; echo "$$" > "$LOCK_NAME") 2> /dev/null; 
 4 then
 5 trap 'rm -f "$LOCK_NAME"; exit $?' INT TERM EXIT
 6 
 7 ### 开始正常流程
 8 
 9 sh /app/system.sh
10 sleep 30
11 ### 正常流程结束
12 
13 ### Removing lock
14 rm -f $LOCK_NAME
15 trap - INT TERM EXIT
16 else
17 echo "Failed to acquire lockfile: $LOCK_NAME." 
18 echo "Held by $(cat $LOCK_NAME)"
19 exit 1
20 fi
21 echo "Done."
22  
posted @ 2019-12-26 09:13  Leonardo-li  阅读(790)  评论(0编辑  收藏  举报