Sending Cron Job emails on Linux
A long time ago, I wrote a post on my WordPress blog about setting SMTP for sending emails when using cron jobs. Now this still works, but it's over five years old, and SMTP has now been replaced by MSMTP, at least on my Arch PC, my Debian server and CachyOS Linux that I'm playing with on a third PC
So let's warp drive into setting it up....
Packages to install
So let's warp drive into setting it up....
Packages to install
- You need to install both the msmtp and the msmtp-mta packages
Configuration
- You need to create a file in /etc called msmtprc
- I use sudo nano /etc/msmtprc (I'm old school and I do not like vim, can never get my head around using it, nano is so much easier )
- I'm using Gmail for this, but just change it if you're using something else. Many emails want an app password rather than the email password, so check on that.
When opening sudo nano /etc/msmtprc, which will give you an empty window, just copy the below section into it, change the email address and password. CTRL O, CTRL X to save and exit
# Gmail account configuration account gmail host smtp.gmail.com port 587 auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt from youremail@@gmail.com user youremail@@gmail.com password 123456 #password replace your actual gmail password with an app password (You are using an app password, I hope, it's so much safer) logfile ~/.msmtp.log # Set the default account to use account default : gmail
After creating the msmtpr file, run this in terminal
sudo chown root:root /etc/msmtprc
sudo chmod 600 /etc/msmtprc
Unless I've forgotten something, that's all that needs to be done
Below is a simple cron job, running at 16h07 every day, to copy/backup all folders and files in your Documents folder to another disc
Obviously, only #3 is what you need to add to an existing cron job that you have
sudo chown root:root /etc/msmtprc
sudo chmod 600 /etc/msmtprc
Unless I've forgotten something, that's all that needs to be done
Below is a simple cron job, running at 16h07 every day, to copy/backup all folders and files in your Documents folder to another disc
Obviously, only #3 is what you need to add to an existing cron job that you have
- (Time) 07 16 * * * root bash -c
- (Cron job, from- to) 'rsync -avhW /home/user/Documents/* /run/media/trevor/Data/
- (Mail command, same email as you inputted into the msmtprc file); echo -e "Subject: Documents Copy Completed\nFrom: yourmail@gmail.com\nTo: yourmail@gmail.com\n\nRsync finished at $(date)" | msmtp --file=/etc/msmtprc yourmail@gmail.com'
A simple cron job, I called it Copy_my_Documents_folder. Use sudo nano /etc/cron.d folder/Copy_my_Documents_folder
#Copy the Documents folder daily to the Data SSD and send an email SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin 07 16 * * * root bash -c 'rsync -avhW /home/user/Documents/* /run/media/user/Data/; echo -e "Subject: Documents Copy Completed\nFrom: yourmail@gmail.com\nTo: yourmail@gmail.com\n\nRsync finished at $(date)" | msmtp --file=/etc/msmtprc yourmail@gmail.com'
PS: I wrote 'user', obviously, you replace that with your user name