Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Shell 将标准输出重定向到文件并添加时间戳_Shell_Scripting - Fatal编程技术网

Shell 将标准输出重定向到文件并添加时间戳

Shell 将标准输出重定向到文件并添加时间戳,shell,scripting,Shell,Scripting,我发现此脚本()用于重定向脚本/程序的stdout/stderr并添加时间戳: #!/bin/bash while read line ; do echo "$(date): ${line}" done 使用此脚本(predate.sh)时,如 它工作得很好,但是当MyApp崩溃或被杀死时,predate.sh脚本会持续运行,并且不会终止。有人知道如何解决这个问题吗 可能存在缓冲问题。我认为这会对你有用: producer | stdbuf -oL gawk '{print strft

我发现此脚本()用于重定向脚本/程序的stdout/stderr并添加时间戳:

#!/bin/bash
while read line ; do
    echo "$(date): ${line}"
done
使用此脚本(predate.sh)时,如


它工作得很好,但是当MyApp崩溃或被杀死时,predate.sh脚本会持续运行,并且不会终止。有人知道如何解决这个问题吗

可能存在缓冲问题。我认为这会对你有用:

producer | stdbuf -oL gawk '{print strftime("%F %T"), $0}' > out.log

为您的
读取添加超时,如果超时,请检查MyApp是否正在运行,如果没有,请退出,如果正在运行,请重新读取

read -t 1 line
if [ $? -eq 1 ]; then
    # Timeout - check MyApp ruuning
    ps ... | grep MyApp ...
fi

谢谢,不幸的是,我正在使用嵌入式设备,stdbuf不可用
read -t 1 line
if [ $? -eq 1 ]; then
    # Timeout - check MyApp ruuning
    ps ... | grep MyApp ...
fi