Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自日志记录:将标准输出记录到文件的Busybox shell脚本_Shell_Logging_Fifo_Busybox_Buildroot - Fatal编程技术网

自日志记录:将标准输出记录到文件的Busybox shell脚本

自日志记录:将标准输出记录到文件的Busybox shell脚本,shell,logging,fifo,busybox,buildroot,Shell,Logging,Fifo,Busybox,Buildroot,我的问题涉及jbarlow对以下问题的回答: 我使用了下面列出的建议脚本。我必须使用它,因为我无法访问完整的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 $* >$PIP

我的问题涉及jbarlow对以下问题的回答:

我使用了下面列出的建议脚本。我必须使用它,因为我无法访问完整的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
看起来(对我来说,这方面的知识有限)是tee结束了,父脚本没有消亡。对吗?如果是这样,那么缺少可读文件不应该导致脚本结束吗?tee是以背景为基础的,因此显然它无法控制父对象

作为背景,如果它死了,还有另一个过程会反复调用它。因此,使用相同的文件可能会导致锁定情况。或者rm可能在创建fifo之前发生

我曾考虑过使用带超时的“读取”,但在某些情况下,每次数小时都不会记录任何内容


是否可以修改脚本,使其不会锁定,并且当fifo的一端死亡时脚本将死亡?

SELF\u LOGGING=1 sh$0$*>$PIPE&
应为
SELF\u LOGGING=1 sh$0“$@”>$PIPE&
。当参数中存在应保留的空格时,差异很重要。不过,一般来说,使用
$*
是错误的,而使用
“$@”
更可能是正确的。也有例外,但它们相对较少。额外的观察很好,但我不太确定这就是问题的全部。您的答案似乎正确,但我不确定如果管道出现故障,如何确保应用程序失败。
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