从控制台输出中获取最后 n 行

发布于 2024-10-07 14:41:01 字数 491 浏览 0 评论 0原文

我想制作一个 shell 脚本,它将有效地从 sterr 和 stin 中获取输出到控制台的最后 n 行。我有一个屏幕会话运行一个进程,如果它通过黑客无限循环崩溃,它将重新启动它:

#!/bin/bash
#This script will be started in a screen session
counter=0
while [ $counter -lt 10 ]; do
    ./run_some_process;
     last_output=#GRAB PREVIOUS OUTPUT FROM CONSOLE HERE AND LOG TO FILE
     echo -e "$last_output" >> mylog.txt;
    sleep 5; #sleep for a few seconds before restarting
done

我需要的是第 7 行代码从 stderr 和 stdin 获取最后 10 行左右并将它们附加到日志文件中

I want to make a shell script that will effectively grab the last n lines from sterr and stin that were outputted to the console. I have a screen session running a process that will restart it if it crashes via a hacky infinite loop:

#!/bin/bash
#This script will be started in a screen session
counter=0
while [ $counter -lt 10 ]; do
    ./run_some_process;
     last_output=#GRAB PREVIOUS OUTPUT FROM CONSOLE HERE AND LOG TO FILE
     echo -e "$last_output" >> mylog.txt;
    sleep 5; #sleep for a few seconds before restarting
done

What I need is for the 7th line of code to grab the last 10 or so lines from stderr and stdin and append them to a log file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-10-14 14:41:01
./run_some_process 2>&1 | tail -10 >>logfle

tail -10 将为您提供最后十行,2>&1 将 stderr 重定向到 stdout,>>logfle 附加到日志文件。

./run_some_process 2>&1 | tail -10 >>logfle

tail -10 will give you last ten lines, 2>&1 redirects stderr to stdout, >>logfle appends to logfile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文