Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash find and email script [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
axelmasok
Apprentice
Apprentice


Joined: 08 Oct 2003
Posts: 171

PostPosted: Tue Jun 02, 2009 12:23 am    Post subject: Bash find and email script [solved] Reply with quote

Hello everyone,

Anyone know a nice way of scripting a find command that results in an email being sent?
If the results from the find are emailed, even better.

The simple find command is as follows:

Code:
find /opt/ -iname *_0


In the past I have emailed with something like this:

Code:
cat $LOGFILE | mailx -s "File found!" am@whatsupdoc.com >/dev/null


I tried combining these 2 into a one liner using just find but there is way too much piping and fancy stuff for it to work.
Can anyone offer a better way?


Last edited by axelmasok on Tue Jun 02, 2009 2:18 am; edited 1 time in total
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3256

PostPosted: Tue Jun 02, 2009 12:58 am    Post subject: Reply with quote

If you have set up /etc/ssmtp/ssmtp.conf properly (note that you will need to give your login and password in that file in plaintext!) then you can do something like this:
Code:
echo -e "To: id@domain.com
Subject: Find output



$( [find command] 2>&1 )
" | /usr/sbin/sendmail -t


EDIT: Nevermind, I misread your post. You want the mail sent only if some output is obtained.
_________________
emerge --quiet redefined | E17 vids: I, II | Now using kde5 | e is unstable :-/
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21619

PostPosted: Tue Jun 02, 2009 1:30 am    Post subject: Reply with quote

How about this:
Code:

find /opt/ -iname '*_0' | ( read line; test -z "$line" && exit 0; ( echo -e "Some files were found:\n$line" ; cat) | mailer)


Replace mailer with whatever invocation you need to start the mailer and set the destination address.
Back to top
View user's profile Send private message
Mad Merlin
Veteran
Veteran


Joined: 09 May 2005
Posts: 1155

PostPosted: Tue Jun 02, 2009 1:53 am    Post subject: Reply with quote

Are you running this via cron? If so, just output directly to stdout and cron will mail it to you if there's any output (you may have to set MAILTO=spam@me.here at the top of your crontab).
_________________
Game! - Where the stick is mightier than the sword!
Back to top
View user's profile Send private message
axelmasok
Apprentice
Apprentice


Joined: 08 Oct 2003
Posts: 171

PostPosted: Tue Jun 02, 2009 2:11 am    Post subject: Reply with quote

Mad Merlin wrote:
Are you running this via cron? If so, just output directly to stdout and cron will mail it to you if there's any output (you may have to set MAILTO=spam@me.here at the top of your crontab).


Good point. I'm still playing with Hu's suggestion. If I can't get Hu's working (with my mailer) I'll give up and let cron do it. I guess more to the point Hu's command can be used elsewhere which will be cool.
Thanks for the suggestions!
Back to top
View user's profile Send private message
axelmasok
Apprentice
Apprentice


Joined: 08 Oct 2003
Posts: 171

PostPosted: Tue Jun 02, 2009 2:18 am    Post subject: Reply with quote

Rightio, I got it working the way I like it.
Thanks Hu and others for taking the time to help out!

Code:
find /opt/ -iname '*._0" | ( read line; test -z "$line" && exit 0; ( echo -e "Somefiles were found:\n$line" ; cat) | mailx -s "Kerio Mailserver has another damaged Calendar. Thanks for the bugs Kerio" am@youripper.com)


Regards
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum