flock命令提供了文件锁的功能。命令参数如下:
[hailong@vhost ~]$ flock -h
flock (util-Linux 2.13-pre7)
Usage: flock [-sxun][-w #] fd#
flock [-sxon][-w #] file [-c] command…
-s –shared Get a shared lock
-x –exclusive Get an exclusive lock
-u –unlock Remove a lock
-n –nonblock Fail rather than wait
-w –timeout Wait for a limited amount of time
-o –close Close file descriptor before running command
-c –command Run a single command string through the shell
-h –help Display this text
-V –version Display version
使用flock控制并发冲突,我们的crond配置如下:
*/1 * * * * root flock -xn /tmp/mytest.lock -c 'php ./test.php'