2010-12-14 80 views
19

我想制作一个shell脚本,它可以有效地抓取输出到控制台的sterr和stin中的最后n行。我跑,如果它通过哈克无限循环崩溃,将重新启动的过程的屏幕会话:从控制台输出抓取最后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和标准输入的最后10行左右并把它们添加到一个日志文件

回答

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

tail -10会给你最后十行,2>&1标准输出重定向到标准错误,>>logfle追加到日志文件。

+1

谢谢!一个小改变:./run_some_process 2>&1 | tail -10 >> logfle看起来像stderr需要去标准输出,所以它会使它到管道。 – Hersheezy 2010-12-15 01:21:18

+0

噢,对不起,我的错误^^“ – 2010-12-15 09:08:09