You are not logged in.
Hi all,
trying to pimp my crontab, I encountered some difficulties. I want to process logfiles (msmtp and procmail for what it's worth, with grep and mailstat) and have the results in my mailbox. Of course only if there are any.
So, here's one of my scripts:
#!/bin/dash
LOGFILE=$HOME/Maildir/_msmtp.log
SUCCESS="exitcode=EX_OK"
egrep -v $SUCCESS $LOGFILE | /usr/bin/mailx -E -s "[SMTP Warning]" $USER
The only reason I am using mailx-heirloom is the "-E" flag which tells it to silently discard mails with an empty body. If only I could break the pipe in the grep step when this yields no lines, I could use any other mail program (sendmail/esmtp).
Does anyone know how I should go about this?
(The other one would go like this:
#!/bin/dash
LOGFILE=$HOME/Maildir/_procmail.log
mailstat -s $LOGFILE | /usr/bin/mailx -E -s "[Procmail Summary]" $USER
)
Thanks for any help,
Andreas
Offline
I'm not sure of a clean way to do this. You could write the output to a temporary file and check the contents before mailing.
Also, fcron is able to mail the output of a command when it returns a non-zero exit status.
Offline
the cleanest way i can come up with:
#!/bin/bash
mymail() {
local output
output="$(cat /dev/stdin)"
[[ -n "$output" ]] && echo $output | mail
}
grep 'foo' ./file | mymail
at first i tried inserting a sorta check function into the pipeline that would call exit on empty input; that didn't work b/c bash executes each link in a pipeline in its own subshell. this means any call to exit only affects that link.
this is why you need to wrap mail, that's the link you want to control, so put your logic there.
as always, untested.
//github/
Offline
Great. Starting from brisbin33's suggestion, I have gotten some results. If it weren't for the necessary formail tinkering to get the mail in proper format, I would already post my solution. But as far as the question of the thread is concerned, the solution is brinbin33's.
Thanks a lot.
Andreas
Offline
You could also install "moreutils" and use the "ifne" command.
grep 'foo' ./file | ifne mail
Offline
You could use a regular 'if statement'. It's not the most efficient, but it's simple.
if egrep -v $SUCCESS $LOGFILE ; then egrep -v $SUCCESS $LOGFILE | /usr/bin/mailx -E -s "[SMTP Warning]" $USER ; fi
Last edited by jadamson (2010-06-14 17:10:37)
Offline