这可能是一个非常简单的问题,但我不明白为什么它会这样.当我调用
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"