flock
命令Linux下的文件锁,一般和crontab一起使用,防止重复执行
help
$ flock --help
Usage:
flock [options] <file>|<directory> <command> [<argument>...]
flock [options] <file>|<directory> -c <command>
flock [options] <file descriptor number>
Manage file locks from shell scripts.
Options:
-s, --shared get a shared lock
-x, --exclusive get an exclusive lock (default)
-u, --unlock remove a lock
-n, --nonblock fail rather than wait
-w, --timeout <secs> wait for a limited amount of time
-E, --conflict-exit-code <number> exit code after conflict or timeout
-o, --close close file descriptor before running command
-c, --command <command> run a single command string through the shell
-F, --no-fork execute command without forking
--verbose increase verbosity
-h, --help display this help
-V, --version display version
For more details see flock(1).
参数说明:
-s, --shared
获取一个共享锁-x, --exclusive
获取一个排它锁-u, --unlock
手动释放锁,一般情况下系统会自动解锁-n, --nonblock
非阻塞模式,当获取锁失败时,返回 1-w, --timeout <secs>
设置阻塞时间,当超时时,退出阻塞模式,返回 1-o, --close
close file descriptor before running command-c, --command <command>
shell 命令
示例
* * * * * (flock -xn /tmp/test.lock -c "bash <path-of>/test.sh")