以下内容将stdout写入日志文件并打印stderr:
bash script.sh >> out.log
这又将stdout和stderr写入日志文件:
bash script.sh >> out.log 2>&1
如何结合这两个function,以便stdout和stderr被logging到一个文件,stderr通过电子邮件发送到我的收件箱?
bash script.sh 2>&1 >> out.log | tee -a out.log
首先,我将stdout重定向到文件stderr到stdout ( stdout行到out.log文件和stderr到管道)。
tee
命令打印标准 输出到标准输出和文件(与字母T相似)。 因此,第二,我打印原始标准错误到stdout和out.log文件( -a
参数意味着追加 )。
您可以将stdout保存在单独的文件和stderr文件中:
0 * * * * bash script.sh > out.log 2> err.log
然后给自己发电子邮件err.log
文件。
这是一个可行的解决方案:
./myscript.sh &> all.txt 2> stderr.txt
&> all.txt
同时具有stderr
和stdout
2> stderr.txt
只有stderr
然后就可以用这些文件做任何你想做的事情,例如电子邮件日志记录!
使用流程替换,您可以尝试:
0 * * * * bash script.sh >> out.log 2> >(exec tee >(exec cat >> mail))
要么
0 * * * * bash -c 'exec bash script.sh >> out.log 2> >(exec tee >(exec cat >> mail))'
exec cat >> mail
模仿邮件。 将其替换为实际进行邮件发送的命令。