How to change the subject when sending email using crontab?

Viewed 947

I have my crontab configured as

MAILTO=example@gmail.com

#1am every day (EST)
00 05 * * * /bin/bash /home/ubuntu/mongo_backup.sh

which sends me email with the subject

Cron <ubuntu@ip-XXX-XX-XX-XX> /bin/bash /home/ubuntu/mongo_backup.sh

But I need with some custom date

<Date> MyProjectName
3 Answers

Better of doing it with a mail package in your os as following. it will also pipe any error output of your mongo_backup script to email as message and email subject will be current date-time.

00 05 * * * /bin/bash /home/ubuntu/mongo_backup.sh 2>&1 | mail -s $(date "+%Y%m%d-%H%M%S") example@gmail.com 

If you want to send the actual date in your message or subject use the date instruction between backquote:

`date`

For example this script will send the current date as subject:

echo "Current date is in the subject of this message" | mail xxx@mydomain.org -s "send at `date`"

You can use Linux inbuilt Mail User Agent Utility - mailx, it can also pipe the output of your shell script, if any, to the specified mail-id.

00 05 * * * /bin/bash /home/ubuntu/mongo_backup.sh | mailx -m -s "$(date +\%Y\%m\%d) MyProjectName" <User-ID here> 2>/dev/null
Related