我需要将screen
的整个输出保存到文件中以便稍后检查所有内容。
原因是我正在通过串口转储闪存,使用屏幕与它连接。
我想将它保存到文件中以检查内存结构。
我试过了 :
$: screen /dev/ttyUSB0 115200 >> foo.txt
$: screen /dev/ttyUSB0 115200 | tee foo.txt
我也试过从屏幕上使用缓冲区文件,但我不明白如何使用它。
有一个简单的方法吗?
有一个用于记录的命令行选项。输出保存到screenlog.n文件,其中n是屏幕的编号。从屏幕的手册页:
'-L'告诉屏幕打开窗口的自动输出记录。
您还可以使用Control-a + H将记录保存到screenlog.n文件中。再关闭一个Control-a + H。
C-a H:开始/结束当前窗口到文件“screenlog.n”的记录。
选定的答案在多个会话中不能很好地工作,并且不允许指定自定义日志文件名。
对于多个屏幕会话,这是我的公式:
1)为每个进程创建一个配置文件:
logfile test.log
logfile flush 1
log on
logtstamp after 1
logtstamp string "[ %t: %Y-%m-%d %c:%s ]\012"
logtstamp on
如果你想“在飞行中”,你可以自动更改logfile
。 \012
的意思是“新行”,因为使用\n
会将其打印在日志文件中:source。
2)使用“-c”和“-L”标志启动命令:
screen -c ./test.conf -dmSL 'Test' ./test.pl
而已。第一次刷新后你会看到“test.log”:
...
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 ]
...
我发现即使配置文件中有“登录”,仍然需要“-L”。
我找不到屏幕使用的时间格式变量列表(如%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中创建一个日志文件
对于mac终端:
script -a -t 0 out.txt screen /dev/ttyUSB0 115200
细节
script
内置应用程序“制作终端会议的打字稿”-a
附加到输出文件-t 0
写入输出文件之间的时间是0秒,因此每个新char都会更新out.txtout.txt
只是输出文件名screen /dev/ttyUSB0 115200
- 用于连接外部设备的命令然后,您可以使用tail来查看文件正在更新
尾巴-100 out.txt
以下命令适用于屏幕版本4.06.02
screen -L -Logfile Log_file_name_of_your_choice command_to_be_executed
从屏幕的手册页:
-Logfile file : By default logfile name is "screenlog.0".
You can set new logfile name with the "-Logfile" option.
您可以使用screen -version检查现有版本的屏幕。您可以从https://www.gnu.org/software/screen/下载并安装最新的屏幕版本。
Ctrl + A然后Shift + H适合我。您可以在程序仍在运行时查看文件screenlog.0
。
Unix下的'script'命令应该可以解决问题。只需在新控制台启动时运行它就应该很好。
以下可能有用(测试:Linux / Ubuntu 12.04):
cat /dev/ttyUSB0
使用上述内容,您可以执行所需的所有重定向。例如,要在保存到文件的同时将输出转储到控制台,您需要执行以下操作:
cat /dev/ttyUSB0 | tee console.log
这是一个技巧:将它包装在sh -c
中!
screen sh -c './some-script 2>&1 | tee mylog.log'
其中2>&1
将stderr重定向到stdout,因此tee
可以捕获并记录错误消息。