我的问题与jbarlow在以下问题上发布的答案有关: redirect COPY of stdout to log file from within bash script itself
我使用了下面列出的建议脚本。我必须使用它,因为我无法访问完整的bash(因为jbarlow指出),因为我使用的是busybox的buildroot版本。
#!/bin/sh
if [ "$SELF_LOGGING" != "1" ]
then
PIPE=tmp.fifo
mkfifo $PIPE
# Keep PID of this process
SELF_LOGGING=1 sh $0 $* >$PIPE &
PID=$!
tee logfile <$PIPE &
# Safe to rm pipe because the child processes have references to it
rm $PIPE
wait $PID
# Return same error code as original process
exit $?
fi
我发现的问题是,似乎某些内容可能会从此脚本中冻结。例如,使用上面代码的冻结脚本的strace如下所示:
Process 29750 attached - interrupt to quit
open("/tmp/tmp.fifo", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
write(2, "/usr/bin/runStuff", 24) = 24
write(2, ": ", 2) = 2
write(2, "line ", 5) = 5
write(2, "45", 2) = 2
write(2, ": ", 2) = 2
write(2, "can't open ", 11) = 11
write(2, "/tmp/tmp.fifo", 21) = 21
write(2, ": ", 2) = 2
write(2, "no such file", 12) = 12
write(2, "\n", 1) = 1
stat64("/sbin/tee", 0xbff7c20c) = -1 ENOENT (No such file or directory)
stat64("/usr/sbin/tee", 0xbff7c20c) = -1 ENOENT (No such file or directory)
stat64("/bin/tee", 0xbff7c20c) = -1 ENOENT (No such file or directory)
stat64("/usr/bin/tee", {st_mode=S_IFREG|0755, st_size=18956, ...}) = 0
_exit(1) = ?
Process 29750 detached
对我来说(对我而言,知识有限)看起来是三通结束而父脚本不会死。那是对的吗?如果是这样,不应该缺少可读文件导致脚本结束? tee是背景的,所以显然无法控制父母。
作为背景,还有另一个进程,如果它死了就会反复调用它。因此,使用同一文件可能导致锁定情况。或者也许rm正在发生之前创建了fifo?
我考虑过使用“读取”超时,但可能会出现一次几小时没有记录任何内容的情况。
是否可以修改脚本以防止锁定,如果/当一个的fifo末端死亡时,脚本将会死亡?