in need of a script to grep a string in logs - then email
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
in need of a script to grep a string in logs - then email
. . . . but after just a handfull of lines, say two or three, i would want it to stop emailing. I'll cron it every minute but then I don't want it to keep sending me alerts after 3 or 4 events since i'll know already.
the -mmin parameter would be so after some minutes I wouldn't get kept being emailed.
. . . . can grep do this without find? so after greping so many instances of the string, i wouldn't want it to keep emailing me since i'll be doing thing cron every minute. again, an email a minute would be too much. and i'd want to do it every minute since i'd like to know soonest.
that looks strange for me. the log files will contain the info regardless of the find command. grep will also find those lines. So you need to restrict grep to find only the latest lines. We would need some info about those lines to help you further
If the log recs are timestamped, you'd keep track of that, otherwise you'd have to count the lines in the file and allow for logfile reset ie start a new shorter file.
Don't keep firing up a new process env every minute, its a load on the system and unnecessary. Write a daemon and have it loop every minute. This also makes tracking easier, because you can keep the info in memory.
You might want to keep latest 'mark' in a small file in case you need to restart the daemon.
If the log recs are timestamped, you'd keep track of that, otherwise you'd have to count the lines in the file and allow for logfile reset ie start a new shorter file.
There is a tool called logtail that does that. It may work similar to the "logtail" from the (way) older logcheck package.
Apart from that I'd urge the OP to curb his wheel re-invention urges as it's not like tools incorporating functionality like that don't exist already...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.