bash的羊群:如果出口不能获得锁

编程入门 行业动态 更新时间:2024-10-13 10:30:25
本文介绍了bash的羊群:如果出口不能获得锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的锁机制用于preventing从并行运行的cron 工作

#!/斌/庆典回声临界区之前(    涌向-e 200    回声在关键节    睡眠5)200 GT; /tmp/blah.lockfile回声临界区后

当运行两个实例togeather,后来的等待,直到第一饰面,然后运行。这可能会导致脚本等待运行的积压。

我如何改变这个脚本,这样,如果羊群无法获取锁,它终止脚本?我试过 -n 没有成功。

解决方案

羊群-n -e 200 || 1号出口

羊群-n 告诉你它未能通过返回失败code(零以外的东西)。你可以做,而不是在你的脚本,使当它看到任何错误选中它退出的顶部设置-e 。

根据您的应用程序时,您可能希望 0退出来表示成功时,不能获取锁。

The following lock mechanism is used for preventing a cron job from running concurrently:

#!/bin/bash echo "Before critical section" ( flock -e 200 echo "In critical section" sleep 5 ) 200>/tmp/blah.lockfile echo "After critical section"

When running two instances togeather, the later waits until the first finishes, and then runs. This can cause backlogs of scripts waiting to run.

How do I alter this script so that if flock can't acquire the lock, it terminates the script? I've tried -n without success.

解决方案

flock -n -e 200 || exit 1

flock -n tells you it failed by returning a failure code (something other than zero). You could instead do set -e at the top of your script to make it exit when it sees any unchecked error.

Depending on your application, you might want to exit 0 to indicate success when the lock can't be acquired.

更多推荐

bash的羊群:如果出口不能获得锁

本文发布于:2023-11-25 20:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:羊群   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!