A process creates a new log file each time it runs. If the log file contains the string ERROR, I want it to be emailed to me using mail. I do not want an email if there were no errors.
Created alias named admins in .mailrc for recipients
This command works fine to always send the log via email:
cat /home/user/import.log | mail -s 'Import Error Log' admins
Now then what I want is a concatenated command to check if the file contains 'ERROR' then run the mail command only if it does.
Thanks in advance.
if fgrep ERROR /home/user/import.log; then
mail -s 'Import Error Log' admins
fi
Related
I'm trying to run a sendmail command in a Red-hat Linux environment on a bash shell script through a cronjob. I can run this script successfully when it is ran manually and every other job within the shell runs correctly other than the mailing part.I have never used sendmail and am not sure if I need to restructure how it is being presented.
I have tried mail and mailx. I am able to send the emails but the log file contain many weird characters that it put the text format into a att00001.bin attachment on the email which I do not want. The sendmail command seems to be the only one that doesn't send an attachment when ran manually. Other cron jobs works correctly and are able to send emails they just do not have the special characters in the log file.
echo '##################################################'
date
echo '##################################################'
#Run Script and write to log file
/comp/gfb281m.sh > /usr/local/bin/oracle/getload/getload.log 2>&1
#Send log file to developer group
(echo "Subject:GetLoad Shell"; echo; cat
/usr/local/bin/oracle/getload/getload.log) | sendmail -v
exampleEmail#outlook.com exampleEmail2#mail.mil
When ran this cron job should send the contents of the getload.log file to a group a of users.
Fixed the issue thanks to another source. I was not using the sendmail's full path. I was just stating "| sendmail -v email" and not sendmails full path which was "/usr/sbin/sendmail" for me. Not sure if links are allowed here but below is where I found the answer.
https://www.unix.com/red-hat/271632-bash-sendmail-command-not-found.html
crontab sets PATH to /usr/bin:/bin. To avoid typing absolute command names like /etc/sbin/sendmail you can set up the PATH in you crontab:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/30 * * * * sendmail user#example.com%subject: Sample email%%Email body%
I am writing a bash script that outputs server status results and want to have a way for it to put the results into a text file then email it to an email entered depending on user input. How would I go about doing this?
Thanks for the help!
to output the results to a file, use the >> or > to at the end of your command followed by a file name.
>> will append to the file. Which means the previous content of your file will remain intact.
> will remove everything and create a file with the new ouput.
for eg:
echo $serverOutput>output.txt
now to mail it :
read email // will read the email address of the user.
to mail:
uuencode output.txt output.txt|mailx -s "server output" $email
your file will be sent as an attachment.
Is this what you require?
Creating a cron job would be another task. But for your initial question about having the serverStatus inside a text file and then mail that to the user's email. Below is an example:
say we use "ps -ef|grep processname" command. replace "top" with the command you have.
ssh -q username#serverName "ps -ef|grep -i process">>output.txt
echo "enter email:"
read email
uuencode output.txt output.txt|mailx -s "serverStatusReport" $email
if you have any script/template which needs to be given this functionality, please share the code or elaborate a bit.
Regarding the cron job you would need to include the command/script in the crontab file along with the time you want it to schedule.
for eg:
echo "* * * * * /path/to/your/script.sh ">>crontab
Hope this gives you a path forward on your task
I am trying to send several attachments from Crontab after my SAS job completes.
My sas job output several .csv files and I'd like to attach them to the email crontab sends.
How do I this?
This code
echo "files attached" | mutt -a "file.csv" -s "week 1 day 1 files"
gives me this error:
/prod/file/sas-data2/monthly_goaling_process/macros/week1_day1_process: line 24: mutt: command not found CONFIDENTIALITY NOTICE:
The error message is clear :
mutt: command not found
means that you have to install this app with your package manager, depends of your Linux flavor.
Another approach is to use sendmailcommand with uuencode (this later solution is more 'light')
I am aware that I can use the following command from my linux terminal to send an email:
sendmail user#example.com < file1.text
This command will basically send the contents of the file1.text as mail body.
But how can I add contents of multiple files inside mail body using the same command?
I tried the following which obviously did not work:
sendmail user#example.com < file1.text,file2.text
Use the cat command. cat is short for "concatenate"; it will concatenate all the files given on the command line. Then pipe the output to sendmail:
cat file1.text file2.text | sendmail user#example.com
To send mail I followed the link https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/ .
When I enter the command
echo "My message" | mail -s subject myemailID#gmail.com
then no error displays but I am not getting the mail. I am attaching screenshot of file /etc/postfix/main.cf.
If you need to use your own mail server, you need to reconfigure it as an internet mail server. In debian like systems you can make it via shell like this:
sudo dpkg-reconfigure postfix
After this you will receive mails but i think you will can find them in spam dir.
But if you need simply to send mails from command line i recommend you to use another one tool - mutt (read this: https://www.linux.com/training-tutorials/how-installsetup-mutt-gmail-centos-and-ubuntu/).