可以把shell文件 xxxxx.sh 写成如下所示
#!/bin/bash
file="/home/hadoop/tmp.lock"
exec 3<>file
flock -n 3 || {
echo "程序正在运行,不要重复执行"
exit 1
}
while true;do
echo 111;
sleep 10s;
done
exec 3>&-
如果需要写在 crontab 里面 ,每分钟执行
*/1 * * * * flock -xn /home/hadoop/tmp.lock -c xxxxx.sh
xxxxx.sh文件里面把锁去掉,改成
#!/bin/bash
while true;do
echo 111;
sleep 10s;
done
exec 3>&-