How to send the contents of a file or its part to the mail from the bash console

Lecture



How to send a message from the bash console to Linux? Everything is very simple! Logs, reports will come to your e-mail instantly, and you will receive information in a timely manner and will be able to instantly respond to errors.

When you have many linux servers and each of them have scripts running on a cron schedule, it’s difficult to control the process of their work, and most importantly, to see the result, be it backup, synchronization, scheduled maintenance, e-mail queues, and so on. The easiest way to check is to receive a ready-made log by e-mail in a clear form. It takes a minimum of traffic, notifications arrive on modern smartphones within a few seconds, which seems to me very convenient.

There are many ways to send messages from the bash console, but I use this one:

1.способ

cat /var/log/backup.log | mail -s "backups log" admin@email.org

2. way - sending part of the file to the mail and executing a command to form a topic

cat /var/www/logs/antivirus.log | sed -n -e '7p' | mail -s "Result Scan sildesign $ (date)" admin @ mail.org

3. way

mail -s "Result Scan sildesign $ (date)" admin@mail.ru

the same command can be inserted in cron

30 3 * * * mail -s "Result Scan sildesign $ (date)" user@mail.ru

so that the letters go away:

1) on the server, you need to install postfix and configure it to send emails.

2) if your letters fall into spam, and you are not burning with the desire to bother with dkim-signatures and dns-records, you can add the email address from which you received the report to the white list.

And one non-essential minus: if there is Cyrillic in the file, then hieroglyphs will come. I did not solve the problem with the encoding (yes, I am a sloth), so everything in the log is written in bourgeois, including transliteration. If in the comments you indicate to me the correct Jedi way of specifying the UTF8 encoding, I will be very grateful.

created: 2016-03-24
updated: 2021-07-14
132547



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system