gpt4 book ai didi

bash - 在 Makefile 中锁定整个目标

转载 作者:行者123 更新时间:2023-11-29 09:27:33 26 4
gpt4 key购买 nike

我有一个 Makefile,其目标执行多个系统范围的操作(例如安装包),所以我想以其他 make 进程等待该目标的方式锁定目标,从而防止并行执行。

我尝试按照 this answer 中的解释使用 flock ,但我认为 makefile 的特殊性阻碍了这一点。

这是我现在拥有的:

LOCKFILE=/var/lock/makefile.lock
LOCKFD=200

mytarget:
# make sure to release the lock in any case
eval "exec $(LOCKFD)>$(LOCKFILE)"; trap "flock -xn $(LOCKFD) && rm -f $(LOCKFILE)" EXIT
# get exclusive waiting lock
flock -x $(LOCKFD)
[regular target operations...]
# release lock and delete the lock file
flock -u $(LOCKFD); flock -xn $(LOCKFD) && rm -f $(LOCKFILE)

失败并出现此错误,因为文件描述符 200 未正确设置:

$ make mytarget
# make sure to release the lock in any case
eval "exec 200>/var/lock/makefile.lock"; trap "flock -xn 200 && rm -f /var/lock/makefile.lock" EXIT
# get exclusive waiting lock
flock -x 200
flock: 200: Bad file descriptor
Makefile:57: recipe for target 'mytarget' failed

eval 定义一定有问题,但我看不出是什么。

最佳答案

Make 在新的 shell 中运行每个命令行。我猜你的 fd 在 eval 之后消失了。您可以使用单个命令行并使用“;”分隔命令。如果您想为了清晰起见拆分命令行,您需要以“\”结束除最后一行以外的所有行。请参阅制作手册,Splitting Recipe Lines .或者使用 shell 脚本。

关于bash - 在 Makefile 中锁定整个目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com