如何使用bash tee将stdout和stderr重定向到脚本中具有屏幕的脚本中的文件

时间:2022-04-04 22:09:18

I have a script which needs to run in screen so I included

我有一个需要在屏幕上运行的脚本,所以我包括在内

#!/usr/bin/screen /bin/bash

as the hash bang and it works great. The only problem is that when the script crashes I don't know what happened, the output is lost and all I know is that screen terminated.

作为哈希爆炸,它工作得很好。唯一的问题是当脚本崩溃时我不知道发生了什么,输出丢失了,我所知道的就是屏幕终止了。

My script is interactive so I need to see stdout and stderr in the terminal and I also want stdout and stderr logged in case it crashed.

我的脚本是交互式的,所以我需要在终端中看到stdout和stderr,我也希望stdout和stderr登录以防它崩溃。

I tried to run the script like

我试着运行这样的脚本

./test-screen-in-bash.sh 2>&1|tee test1.log

which results in an empty test1.log file

这导致一个空的test1.log文件

Can somebody please explain what am I doing wrong.

有人可以解释一下我做错了什么。

1 个解决方案

#1


Thanks to @JID comments I was able to find what i was looking for.

感谢@JID的评论,我能够找到我想要的东西。

I removed the screen from hash bang and used the method from the link provided by @JID here in the first answer.

我从哈希爆炸中删除了屏幕,并在第一个答案中使用了@JID提供的链接中的方法。

I ended up with

我结束了

#!/bin/bash
if [ -z "$STY" ]; then exec screen -L /bin/bash "$0"; fi
./myscript.sh

Now when I run the above, myscript.sh runs in screen and the whole output from the session is dumped to screenlog.n files.

现在当我运行上面的命令时,myscript.sh在屏幕上运行,会话的整个输出被转储到screenlog.n文件中。

#1


Thanks to @JID comments I was able to find what i was looking for.

感谢@JID的评论,我能够找到我想要的东西。

I removed the screen from hash bang and used the method from the link provided by @JID here in the first answer.

我从哈希爆炸中删除了屏幕,并在第一个答案中使用了@JID提供的链接中的方法。

I ended up with

我结束了

#!/bin/bash
if [ -z "$STY" ]; then exec screen -L /bin/bash "$0"; fi
./myscript.sh

Now when I run the above, myscript.sh runs in screen and the whole output from the session is dumped to screenlog.n files.

现在当我运行上面的命令时,myscript.sh在屏幕上运行,会话的整个输出被转储到screenlog.n文件中。