My Bash script executes as a cron job. It seeks a string within a log file and is supposed to forward the corresponding log entry by mail if found.

My script works but floods me with already reported log entries containing the same string, but with earlier timestamps. I want to modify it so it only sends the latest timestamp for log entries of interest.

grep "Too many files" /share/logs/access.log > /dev/null && grep "Too many files" /share/logs/access.log | mailx -s "Batch Error " to [email protected]

The corresponding cron job runs every 10 min:

*/10 * * * *  /myscripts/joberror.sh

The log entries I seek in access.log look like:

uzeadnos12 2020-10-14 13:29:00,443 ERROR  - Error occured while converting document using Libreoffice - /shard.tiff java.io.IOException: Cannot run program "": error=24, Too many open files

Can somebody help me improve that script ?

  • 1
    Do you mean to grep "Too many files" or "Too many open files" ?
    – Cbhihe
    Commented Oct 15, 2020 at 7:28

1 Answer 1


Create a receptacle (a file, say ~/spool/last_jobrec) where you can log the last timestamp recorded by your cron job. Compare it to the time stamp of last appended log entry. When the two are different mail a notice, when identical do nothing.

$ mkdir -p /path/to/spool    # choose a place for spool directory.
$ cat /myscripts/joberror.sh
touch /path/to/spool/last_jobrec   # ensure appropriate permissions
latest_logentry=$(grep --no-messages --no-filename --max-count=1 -e "Too many open files" <(tac /share/logs/access.log))
latest_logstamp=$(cut -d' ' -f1,2 <(echo $latest_logentry))
if [ "$latest_logstamp" != "$(cat /path/to/spool/last_jobrec)" ]; then
    echo "$latest_logentry" | mailx -s "Batch Error " to [email protected]
    echo "$latest_logstamp" >| /path/to/spool/last_jobrec

Untested. In particular I don't use the mailx utility.

1) I assume that new log entries are appended at the end of your logfile.
2) To populate the variable last_entrystamp above I used an expression based on grep. It is because I assume conservatively that your log file could contain other entries in the form of lines not containing the string "Too many open files". If not, just replace the grep line in the script with with latest_logentry=$(tail -1 /share/logs/access.log)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .