gpt4 book ai didi

linux - lockfile-create 在 bash 脚本中不起作用

转载 作者:IT王子 更新时间:2023-10-29 00:35:45 30 4
gpt4 key购买 nike

这可能是一个非常简单的问题,但我不明白为什么会这样。当我调用

lockfile-create --use-pid --retry 0 /tmp/my_lock_file

它返回 0,下次运行时它会按预期返回一些其他代码 (4),因为它已经创建了锁定文件。但是当我将相同的代码包装在 bash 脚本文件中时,它总是返回 0 作为退出代码。有人知道为什么它不起作用吗?

更新:完整的bash文件内容

#! /bin/bash

LOCK=alert

lockfile-create --use-pid --retry 0 $LOCK
LOCK_CREATED=$?
echo "Lock file creation status $LOCK_CREATED"

这就是我运行它的方式 ./alert.sh。

最佳答案

But when I wrap that same code in a bash script file, it always returns 0 as the exit code.

这是因为当你再次执行脚本时,执行脚本的进程的PID发生了变化。因此,--use-pid 标志导致 lockfile-create 认为需要覆盖锁定文件。

根据您的用例,您可能希望摆脱 --user-pid 标志。但是,在这种情况下,您需要确保自己清理锁定文件。

引用自 man lockfile-create:

   -p, --use-pid
Write the parent process id (PPID) to the lockfile whenever a lock‐
file is created, and use that pid when checking a lock's validity.
See the lockfile_create(3) manpage for more information. This
option applies to lockfile-create and lockfile-check. NOTE: this
option will not work correctly between machines sharing a filesys‐
tem.

您可以通过尝试在同一脚本中再次创建日志文件来验证您观察到的行为:

#! /bin/bash
LOCK=alert

lockfile-create --use-pid --retry 0 $LOCK
LOCK_CREATED=$?
echo "Lock file creation status $LOCK_CREATED"
lockfile-create --use-pid --retry 0 $LOCK
LOCK_CREATED=$?
echo "Lock file creation status $LOCK_CREATED"

关于linux - lockfile-create 在 bash 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20089797/

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