{"id":254,"date":"2017-09-26T08:53:26","date_gmt":"2017-09-26T00:53:26","guid":{"rendered":"http:\/\/blog.warbel.net\/?p=254"},"modified":"2017-09-26T08:53:26","modified_gmt":"2017-09-26T00:53:26","slug":"how-to-email-system-logs-via-the-terminal-cron-and-smtp","status":"publish","type":"post","link":"https:\/\/blog.warbel.net\/index.php\/2017\/09\/26\/how-to-email-system-logs-via-the-terminal-cron-and-smtp\/","title":{"rendered":"How to Email System Logs via the Terminal,  Cron and SMTP"},"content":{"rendered":"<h1>Background:<\/h1>\n<p>Every day I run a rsync job that transfers backups between two servers. The job is a two-part cronjob. As seen below:<\/p>\n<pre>30 23 * * * \/home\/&lt;user&gt;\/rsync.sh &gt;\/dev\/null 2&gt;&amp;1\r\n0 6 * * * killall rsync &gt;\/dev\/null 2&gt;&amp;1;<\/pre>\n<p>The job starts at 11:30pm and is killed at 6am. The script that it calls does the following:<\/p>\n<pre>#!\/bin\/bash\r\nfind \/var\/log\/rsync\/ -mtime +8 |xargs -I % sh -c 'rm -f %';\r\nfind \/var\/log\/rsync\/log.* |xargs -I % sh -c 'tar -rf \/var\/log\/rsync\/rsync.1.tar %; rm -f %';\r\nrsync --bwlimit=1050 --protect-args --delete --size-only --copy-dirlinks --log-file=\/var\/log\/rsync\/log.`date +\"%Y%m%d_%H%M%S\"` -avP -e \"ssh -T -o Compression=no -x\" \"\/path\/to\/files\/\" \"&lt;user&gt;@domain:\/path\/to\/files\/\";<\/pre>\n<p>Basically it removed old logs, putting them into a nice tarball which it will delete periodically. Then it runs the backup script, creating a new log. Generally, I will log in peridocally and manually check the logs to make sure everything is working as it should. What I want to to do, simply, is to have it email the contents of the log every day, saving me the 30 seconds trouble of logging in and checking manually.<\/p>\n<p>As I have a &#8216;proper&#8217; mail server with SMTP\/IMAP I want to us it to send the logs.<\/p>\n<h1>Installing and Configuring Packages:<\/h1>\n<pre>sudo apt install mailutils ssmtp<\/pre>\n<p>Configure ssmtp by editing the main config file: \/etc\/ssmtp\/ssmtp.conf. Comment out all the other lines so your configuration looks like this:<\/p>\n<pre>mailhub=mailserver.domain.com:587\r\nUseSTARTTLS=YES\r\nAuthUser=user@domain.com\r\nAuthPass=password<\/pre>\n<p>You will need to have configured a mail user on your mail server. All users will send from the user@domain.com address. This isn&#8217;t a problem as the only mail I&#8217;m sending from this server are alerts and logs. In server environments where there are multiple users sending general mail this setup will not be appropriate.<\/p>\n<p>Next, edit the revaliases file in the same directory. Add the details for the user who will be running the command to send email:<\/p>\n<pre>localuser:user@domain.com:mailserver.domain.com:587<\/pre>\n<p>That&#8217;s the configuration done!<\/p>\n<p>Test sending an email with the following:<\/p>\n<pre>echo \"this is a test\" | mail -s \"Test Email\" email@your.address.com<\/pre>\n<p>Check the contents of the syslog:<\/p>\n<pre>:~$ tail -3 \/var\/log\/syslog\r\nSep 26 08:47:21 servername sSMTP[23535]: Creating SSL connection to host\r\nSep 26 08:47:22 servername sSMTP[23535]: SSL connection using RSA_AES_128_CBC_SHA1\r\nSep 26 08:47:25 servername sSMTP[23535]: Sent mail for user@domain.com (221 2.0.0 Bye) uid=1000 username=localuser outbytes=4792<\/pre>\n<p>Success!<\/p>\n<h1>Automate sending the logs:<\/h1>\n<p>Change the crontab file with:<\/p>\n<pre>crontab -e<\/pre>\n<p>Add the email command to the end of the job that kills the process:<\/p>\n<pre>30 23 * * * \/home\/wargus\/rsync.sh &gt;\/dev\/null 2&gt;&amp;1\r\n0 6 * * * killall rsync &gt;\/dev\/null 2&gt;&amp;1; cat \/var\/log\/rsync\/log* | mail -s \"Rsync Log for `date`\" warren@warbel.net<\/pre>\n<h1>Further Reading:<\/h1>\n<p><a href=\"https:\/\/linux.die.net\/man\/8\/ssmtp\">https:\/\/linux.die.net\/man\/8\/ssmtp<\/a><br \/>\n<a href=\"https:\/\/www.nixtutor.com\/linux\/send-mail-with-gmail-and-ssmtp\/\">https:\/\/www.nixtutor.com\/linux\/send-mail-with-gmail-and-ssmtp\/<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/20318770\/send-mail-from-linux-terminal-in-one-line\">https:\/\/stackoverflow.com\/questions\/20318770\/send-mail-from-linux-terminal-in-one-line<\/a><br \/>\n<a href=\"https:\/\/tecadmin.net\/send-email-smtp-server-linux-command-line-ssmtp\/\">https:\/\/tecadmin.net\/send-email-smtp-server-linux-command-line-ssmtp\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background: Every day I run a rsync job that transfers backups between two servers. The job is a two-part cronjob. As seen below: 30 23 * * * \/home\/&lt;user&gt;\/rsync.sh &gt;\/dev\/null 2&gt;&amp;1 0 6 * * * killall rsync &gt;\/dev\/null 2&gt;&amp;1; &hellip; <a href=\"https:\/\/blog.warbel.net\/index.php\/2017\/09\/26\/how-to-email-system-logs-via-the-terminal-cron-and-smtp\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[29,26,30,31,27,28],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cron","tag-email","tag-logs","tag-rsync","tag-smtp","tag-ssmtp"],"_links":{"self":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":1,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}