tags:

views:

61

answers:

2

I have a process that dumps millions of lines to the console while it runs. I'd like to run this in a cronjob but to avoid sending multi-MB mails, I'd like to restrict the output in the case of a success (exit == 0) to 0 lines and in case of an error (exit != 0) to the last 20 lines.

Any ideas to achieve this with little effort? Maybe a few lines of perl or a smart use of standard tools?

+2  A: 

Is the tail command a good fit for what you're trying to do? Maybe if the console output is also available in a file (using tee, maybe)?

lance
+2  A: 

Just pipe output to tail, either directly in the crontab or in a wrapper script. e.g.

10 * * * * myprogram 2>&1 | tail -20

That'll always output the last 20 lines, success or not. If you want no output on success and some on error, you can create a wrapper script that you call from cron e.g.

#!/bin/sh
myprogram 2>&1 | tail -20 >/tmp/myprogram.log
if [ $? != 0 ] ; then 
    echo "Failed!"
    cat /tmp/myprogram.log
fi
rm /tmp/myprogram.log
nos
It's too damn hot in here; I can't think anymore. Of course, a simple tail will do :)
Aaron Digulla