我需要将Screen的整个输出保存到文件中,以便以后检查所有内容。
原因是我通过串口转储闪存,使用Screen与其交互。我想将其保存到文件中以检查内存结构。
我尝试过:
screen /dev/ttyUSB0 115200 >> foo.txt
screen /dev/ttyUSB0 115200 | tee foo.txt
我也尝试过从屏幕使用bufferfile,但是我不明白如何使用它。
有没有更简单的方法?
我需要将Screen的整个输出保存到文件中,以便以后检查所有内容。
原因是我通过串口转储闪存,使用Screen与其交互。我想将其保存到文件中以检查内存结构。
我尝试过:
screen /dev/ttyUSB0 115200 >> foo.txt
screen /dev/ttyUSB0 115200 | tee foo.txt
我也尝试过从屏幕使用bufferfile,但是我不明白如何使用它。
有没有更简单的方法?
有一个用于日志记录的命令行选项。输出结果将保存到screenlog.n文件中,其中n是屏幕的编号。 来自Screen手册:
'-L' 告诉Screen为窗口开启自动输出日志功能。
hardcopy -h <文件名>
以备其他需要者参考。 - Tagarscreen -L /path/to/logfile
这将在运行screen时创建一个名为“/path/to/logfile”的文件,并将该文件用作日志文件。 - lepe您还可以使用Ctrl+A,H将日志保存到screenlog.n文件中。
再次使用Ctrl+A,H关闭日志记录。
Ctrl+A,H: 开始/结束将当前窗口的日志记录到文件“screenlog.n”中。
a
+ :
,然后输入例如 chdir /home/foobar/baz
。 - Chriki以下命令适用于Screen版本4.06.02:
screen -L -Logfile Log_file_name_of_your_choice command_to_be_executed
来自 Screen 的 man 页面:
-Logfile file : By default logfile name is "screenlog.0".
You can set new logfile name with the "-Logfile" option.
您可以使用screen -version检查现有版本的Screen。您可以从https://www.gnu.org/software/screen/下载并安装最新版本的Screen。
选定的答案在多个会话中不太有效,并且不允许指定自定义日志文件名称。
对于多个屏幕会话,这是我的公式:
Create a configuration file for each process:
logfile test.log
logfile flush 1
log on
logtstamp after 1
logtstamp string "[ %t: %Y-%m-%d %c:%s ]\012"
logtstamp on
If you want to do it "on the fly", you can change logfile
automatically.
\012
means "new line", as using \n
will print it on the log file: source.
Start your command with the "-c" and "-L" flags:
screen -c ./test.conf -dmSL 'Test' ./test.pl
That's it. You will see "test.log" after the first flush:
...
6 Something is happening...
[ test.pl: 2016-06-01 13:02:53 ]
7 Something else...
[ test.pl: 2016-06-01 13:02:54 ]
8 Nothing here
[ test.pl: 2016-06-01 13:02:55 ]
9 Something is happening...
[ test.pl: 2016-06-01 13:02:56 ]
10 Something else...
[ test.pl: 2016-06-01 13:02:57 ]
11 Nothing here
[ test.pl: 2016-06-01 13:02:58 ]
...
我发现即使在配置文件中开启了“log on”,仍然需要使用“-L”参数。
我找不到screen使用的时间格式变量(如%m)的列表。如果您有这些格式的链接,请在下面发布。
如果您想要“即时”执行此操作,可以使用以下脚本:
#!/bin/bash
if [[ $2 == "" ]]; then
echo "Usage: $0 name command";
exit 1;
fi
name=$1
command=$2
path="/var/log";
config="logfile ${path}/${name}.log
logfile flush 1
log on
logtstamp after 1
logtstamp string \"[ %t: %Y-%m-%d %c:%s ]\012\"
logtstamp on";
echo "$config" > /tmp/log.conf
screen -c /tmp/log.conf -dmSL "$name" $command
rm /tmp/log.conf
使用它,保存它(screen.sh)并设置+x权限:
./screen.sh TEST ./test.pl
...并且将执行./test.pl,并在/var/log/TEST.log中创建一个日志文件。
on-the-fly
部分非常有用。 - Ram RSscreen -r
时出现错误,显示为“无法打开“/tmp/log.conf””。此外,该屏幕从“[detached]”状态变为不存在。可能是什么问题? - Ram RSscreen bash
相同。如果任何其他进程正在杀死你的屏幕,它应该被列为“死亡”,但不会消失。我不确定它可能是什么。 - lepe现有的屏幕日志可以通过以下方式保存:
Ctrl+A : hardcopy -h 文件名
对于Mac终端:
script -a -t 0 out.txt screen /dev/ttyUSB0 115200
script
: 一个内置应用程序,可以“制作终端会话的typescript”-a
: 追加到输出文件-t 0
: 写入输出文件之间的时间为0秒,因此每个新字符都会更新out.txtout.txt
: 只是输出文件名screen /dev/ttyUSB0 115200
: 从问题中连接到外部设备的命令然后,您可以使用tail查看文件是否正在更新。
tail -100 out.txt
Ctrl+A然后Shift+H对我有效。在程序仍在运行时,您可以查看文件screenlog.0
。
如果您需要保存已经在运行中的屏幕的整个滚动缓冲区的输出,那么这是一个不同的答案:
Ctrl-a [ g SPACE G $ >.
这将把您的整个缓冲区保存到/tmp/screen-exchange中
Ctrl + a
进入命令模式,然后输入:
,接着使用hardcopy -h <filename>
。 - Tarjei Husesh -c
中!screen sh -c './some-script 2>&1 | tee mylog.log'
在 2>&1
中,将标准错误重定向到标准输出,以便 tee
可以捕获并记录错误消息。