看一个项目中用到的shell脚本,使用flock加锁来实现单例模式,
SERVICE_PATH=/root/anshell.sh
umask 000 # allow all users to access the file we're about to create
exec 9>"/tmp/${0##*/}.lck" # open lockfile on FD 9, based on basename of argv[0]
umask 022 # move back to more restrictive file permissions
flock -no 9 || exit # grab that lock, or exit the script early
$SERVICE_PATH 9>&- &
当然还有trap删除这个lck,当脚本退出的时候,对于上面的代码存在一个疑问,最后一行中9>&-是什么意思? 最后一个&是后台运行的意思吧?
9>&- 表示关闭文件描述符9
最后一个& 是后台运行