This is how you edit your cron file:

crontab -e

Here is an example of basic cronjob which dumps the database every 6 hours and sends an email, the email will contains whatever the command outputs to standard output to Tom Hanks and Tom Cruise:

MAILTO="[email protected],[email protected]"
0 */6 * * * mysqldump -u tom_hanks -pHANKS_TOM --host= mycompany > /home/tom_hanks/db.sql

Now for mail command to work, you will need a MTA agent. Install postfix to get the mail command through your terminal in ubuntu/debian:

sudo apt-get update && sudo apt-get install -y postfix mailutils


sudo dnf update && sudo dnf install postfix mailx -y 

replace dnf with yum for older Redhat or Fedora.

For mailx you can install bsd-mailx on ubuntu/debian:

sudo apt-get update && sudo apt-get install bsd-mailx 

If you already have postfix installed, you can troubleshoot using this command in a terminal on ubuntu/debian:

sudo dpkg-reconfigure postfix

Open port 25 using iptables:

iptables -I INPUT -p tcp -m tcp --dport 25 -j ACCEPT

Since you want root to run the cronjob, use

sudo crontab -e

Then add your cronjob like this:

*/5 * * * * sh /scripts/ &> /tmp/myDbBack.log &&  mail -s "Nightly Test Database Back-up Is Running Successfully" [email protected],[email protected] < /dev/null

mail command will need a email body which we are providing null (nothing)

Leave a Reply

Your email address will not be published. Required fields are marked *