TransWikia.com

Discriminating between log entries' timestamps from bash script

Unix & Linux Asked by ragnar on January 24, 2021

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.

#!/bin/bash
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 ?

One Answer

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
#!/bin/bash
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
fi

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

Remarks:
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)

Answered by Cbhihe on January 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP