在bash中创建一个锁定文件,以避免重复执行 [英] create a lock file in bash to avoid duplicate execution
本文介绍了在bash中创建一个锁定文件,以避免重复执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对bash不太好,我一直在修改代码以创建锁定文件,因此如果第一个过程尚未完成,cron不会再次执行.
I'm not very good on bash I've been modifying a code to create a lock file so a cron don't execute a second time if the first process hasn't finish.
LOCK_FILE=./$(hostname)-lock
(set -C; : > $LOCK_FILE) 2> /dev/null
if [ $? != "0" ]; then
echo "already running (lock file exists); exiting..."
exit 1
fi
trap 'rm $LOCK_FILE' INT TERM EXIT
当我第一次运行它时,我得到的消息已经在运行,就好像文件已经存在一样.
when I run it for the first time I get the message already running as if the file already existed.
也许我想念一些东西
推荐答案
#!/bin/sh
(
# Wait for lock on /tmp/lock
flock -x -w 10 200 || exit 127 # you can use or not use -w
#your stuff here
) 200> /tmp/lock
检查手册页群.
这是给您的工具. 它带有手册页中的示例:)
This is the tool for you. And it comes with example in man page :)
这篇关于在bash中创建一个锁定文件,以避免重复执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文