我有一个在我的电脑旁边运行的设备,当它运行时,它会从串行端口输出日志行。我将其连接到我的 PC,如果我使用 minicom 或类似的东西,我可以很好地看到日志行:
ttylog -b 115200 -d /dev/ttyS0
我想将 5 秒的设备串行输出写入临时文件(或将其分配给变量)并且然后在该文件中查找关键字,让我知道设备的运行情况。我已经尝试过在后台运行命令时将输出重定向到文件,然后休眠 5 秒并终止进程,但日志行永远不会写入我的临时文件。示例:
touch tempFile
ttylog -b 115200 -d /dev/ttyS0 >> tempFile &
serialPID=$!
sleep 5
#kill ${serialPID} #does not work, gets wrong PID
killall ttylog
cat tempFile
文件被创建但从未填充任何数据。我还可以将 ttylog 行替换为:
ttylog -b 115200 -d /dev/ttyS0 |tee -a tempFile &
在这两种情况下,我都不会看到任何记录到 stdout 或日志文件的日志行,除非我错误地运行了多个版本的 ttylog(请参阅注释掉的行,D'oh)。
我不知道这里发生了什么事。这似乎是我的脚本中的重定向失败。
我走在正确的轨道上吗?有没有更好的方法来采样5秒的串口?
I've got a device that I'm operating next to my PC and as it runs it's spitting log lines out it's serial port. I have this wired to my PC and I can see the log lines fine if I'm using either minicom or something like:
ttylog -b 115200 -d /dev/ttyS0
I want to write 5 seconds of the device serial output to a temp file (or assign it to a variable) and then later grep that file for keywords that will let me know how the device is operating. I've already tried redirecting the output to a file while running the command in the background, and then sleeping 5 seconds and killing the process, but the log lines never get written to my temp file. Example:
touch tempFile
ttylog -b 115200 -d /dev/ttyS0 >> tempFile &
serialPID=$!
sleep 5
#kill ${serialPID} #does not work, gets wrong PID
killall ttylog
cat tempFile
The file gets created but never filled with any data. I can also replace the ttylog line with:
ttylog -b 115200 -d /dev/ttyS0 |tee -a tempFile &
In neither case do I ever see any log lines logged to stdout or the log file unless I have multiple versions of ttylog running by mistake (see commented out line, D'oh).
I have no idea what's going on here. It seems to be a failure of redirection within my script.
Am I on the right track? Is there a better way to sample 5 seconds of the serial port?
发布评论
评论(3)
听起来也许 ttylog 正在缓冲其输出。您是否尝试过使用 -f 或 --flush 运行它?
It sounds like maybe ttylog is buffering its output. Have you tried running it with -f or --flush?
您可以尝试使用 unbuffer 脚本.gov" rel="nofollow noreferrer">期望。
You might try the unbuffer script that comes with expect.
ttylog
有一个--timeout
选项,您可以在其中简单地指定它应该运行多少秒。因此,就您的情况而言,您可以这样做
,它只会运行 5 秒钟,然后停止。
事实上,它还有前面提到的用于刷新的
-f
选项,但如果您使用--timeout
您就不需要它了。ttylog
has a--timeout
option, where you can simply specify for how many seconds it should run.So, in your case, you could do
and it would just run for 5 seconds and then stop.
Indeed it also has the
-f
option as mentioned which flushes, but if you'd use--timeout
you would not be needing that.