View previous topic :: View next topic |
Author |
Message |
axelmasok Apprentice
Joined: 08 Oct 2003 Posts: 171
|
Posted: Tue Jun 02, 2009 12:23 am Post subject: Bash find and email script [solved] |
|
|
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 |
|
|
ppurka Advocate
Joined: 26 Dec 2004 Posts: 3256
|
Posted: Tue Jun 02, 2009 12:58 am Post subject: |
|
|
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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21619
|
Posted: Tue Jun 02, 2009 1:30 am Post subject: |
|
|
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 |
|
|
Mad Merlin Veteran
Joined: 09 May 2005 Posts: 1155
|
Posted: Tue Jun 02, 2009 1:53 am Post subject: |
|
|
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 |
|
|
axelmasok Apprentice
Joined: 08 Oct 2003 Posts: 171
|
Posted: Tue Jun 02, 2009 2:11 am Post subject: |
|
|
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 |
|
|
axelmasok Apprentice
Joined: 08 Oct 2003 Posts: 171
|
Posted: Tue Jun 02, 2009 2:18 am Post subject: |
|
|
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 |
|
|
|