从控制台输出中获取最后 n 行
我想制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tail -10
将为您提供最后十行,2>&1
将 stderr 重定向到 stdout,>>logfle
附加到日志文件。tail -10
will give you last ten lines,2>&1
redirects stderr to stdout,>>logfle
appends to logfile.