echo text and tail log to pipe to email - email

I want to echo text as well as tail the last part of a log to pipe it to email. I can easily do one or the other, but how do I do both without first writing everything to a file and then emailing that? I basically want to combine the below two commands into one to only send out one email.
echo "There is an error in the log, see the below for detail" | mail -s "error in the log" xxx#yyy.zzz
and
tail -n 10 /var/log/error.log | mail -s "error in the log" xxx#yyy.zzz
Thanks.

You could use the concatenate command along with process substitution:
cat <(echo "There is an error in the log, see the below for detail") <(tail -n 10 /var/log/error.log) | mail -s "error in the log" xxx#yyy.zzz

Related

How To Send email alert on log file entry with awk in Linux?

I'm working on a script to monitor a log of jobs executed and I want to receive a mail notification with the line where appears the job in the body of the mail. This is what I got so far but it keeps throwing error, I can make it work but just with an empty body. Could you please help?
Job="jobname"
tail -fn0 logfile.log | awk -v Jobs="$Job"'/jobname/
{
system("grep -i "Jobs" logfile.log | mail -s "Jobs Is Completed" mail#mail.com")
exit
}'
What's wrong with just:
Job="jobname"
tail -fn0 logfile.log |
grep --line-buffered -i "jobname.*$Job" |
mail -s "$Job Is Completed" mail#mail.com"
Your use of jobname as literal in 2 places and a shell variable named Job with an awk variable named Jobs populated from it and both containing jobname anyway was very confusing so hopefully you can tweak the above to do whatever you need to do if the variable usage above is not quite right.
watchdog.sh
#!/bin/bash
mailWorker(){
while read -r line; do
if [[ $line == *$match* ]]
then
# mailing
grep -i "Jobs" "$logfile" | mail -s "Jobs Is Completed" mail#mail.com
break
fi
done
}
logfile="/path/to/logfile.log"
match="jobname"
if [ ! -f "$logfile" ]; then
touch "$logfile"
fi
tail -f --lines=0 "$logfile" | mailWorker

Check if file transfer to servers failed and send an email for the same

I have a file which I want to save it some other server say abc.com.
In case this operation fails because of some reason like (permission denied or anything else), currently it fails out silently.
But I want , in such situation it shoudl send an email to Ruhi#.abc.com.
How can I implement this and test the scenario.
Excerpt of the code:
sed -e abc.txt > /net/abc.com/test/abc.txt
#Sending email
if [ -f /net/abc.com/test/abc.txt ]; then
echo "content of abc.txt saved to server" | mail -s "Content of abc" dave#klm.com
else
echo "File does not exist"
fi

how to send an email alert if a folder is modified

I want to get an email alert if a certain folder is modified but how do I pipe the output from the command so it sends an email not just show the changes to the folder in the terminal?
something like the following but... gives an error on the email part
inotifywait -m /home/tom -e create -e moved_to |
while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"
| /usr/bin/Mail -s "notify" "email#12345mail.com"
done
Could it be you simply missed the semicolon before done?
This line works for me (note I also used mutt instead of Mail):
inotifywait -m /home/tom -e create -e moved_to | while read path action file; do echo "The file '$file' appeared in directory '$path' via '$action'" | /usr/bin/mutt -s "notify" "email#12345.com" ;done

Mailx - Daily email of log file to list of recipients

I was given a few example usages of mailx and asked to come up with another by my manager.
I have a program that will be left running. Once a day, we want to send the log file it generates to a list of recipients.
This was the closest example I received
00 11 * * * unset noclobber;cat /home/emailList.txt /tools/temp/summary.csv | mailx -t -s "Report `/bin/date`" -r person#company.com > /home/file.log
Seems like I want something like:
MyProgram | mailx -t -s "Report `/bin/date`" /home/emailList.txt -r person#company.com
But here, I'm not sending it once a day. This sends at MyProgram closing, right? I'm also sending MyProgram's output, not it's log file.
Thanks in advance.

inserting system date into subject field of ssmtp email

I'd like to know the syntax for inserting the current date/time into the subject line of an email sent by ssmtp.
I've got a cronjob emailing the tail of my syslog whenever the system reboots.
Here is the the cronjob:
#reboot tail -1000 /var/log/syslog | mail -s "the system rebooted, here's the syslog" address#gmail.com &> /dev/null
Is there a simple way of inserting the system date into the subject line field? I haven't found a way to add it.
You should be able to make it print the current date and time by inserting $(date) into your subject line string.
Try:
#reboot tail -1000 /var/log/syslog | mail -s "$(date): the system
rebooted, here's the syslog" address#gmail.com &> /dev/null