LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [crontab] emailing non-empty results (https://www.linuxquestions.org/questions/programming-9/%5Bcrontab%5D-emailing-non-empty-results-483355/)

G00fy 09-14-2006 02:39 AM

[crontab] emailing non-empty results
 
Hi all,


At the moment I do something like this:
Code:

/bin/sh script.sh | mutt -s Result email@mail.be
But like 80% of the time the result is empty... So is the mail I get... How can I change this so that I only receive non-empty mails?


Thanks!

ramram29 09-14-2006 01:07 PM

You should output the results as /tmp/result.txt then attach them in the message. You should call mutt within the script.sh file at the bottom. You can specify to send result.txt if it is not empty with an 'if' function.

if [ -s /tmp/result.txt ]; then
mutt -s "Result $(date +%F)" email@mail.be < /tmp/result.txt
fi

I recommend a good book on script writing for bash or perl.

G00fy 09-14-2006 03:22 PM

Thanks ramram29, but I was more thinking about some 1 liner. I know you can put all of this on 1 line in the crontab, but I have like 10 entries there mailing me stuff every hour, and about 90 others during the day...

So you can understand that I don't want to type so much, and I'm looking for an easy solution. Also it seems so rough to export to a file, which would need to be a default filename (bad idea ;), better to have random name then), which I just check if empty and remove it... Just doesn't feels good.

Oh, and I have a good book lying here, it's just no use for what I need to do with it. I already found this kind of solution, but threw it away because of the reasons mentioned above.

Thanks anyway :)

chrism01 09-14-2006 06:43 PM

In that case, either add a filter to your smtp server, or write a simple shell prog that you can insert in the pipelines eg
/path/script1.sh|your_filter.sh
where your_filter.sh decides whether the inut requires emailing or not, and takes care of it.
Incidentally, it's better to put the hash-bang line ( #!/bin/bash) at the top of each sh script and just call it without separately invoking the shell eg as per my example

bigearsbilly 09-19-2006 05:29 AM

cron already mails a file only when non-empty?

you don't need to mail the result to yourself.

chrism01 09-20-2006 01:16 AM

I suspect the OP is trying to email a 'remote user' ie not the local user acct that is actully running the cron job.


All times are GMT -5. The time now is 06:14 AM.