3

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
Profer
  • 553
  • 8
  • 40
  • 81
  • do any of these methods work on your system? [link1](https://stackoverflow.com/a/50424331/10304821), [link2](https://serverfault.com/questions/56497/how-to-modify-a-cronjob-email-subject) – Milag Oct 07 '20 at 12:56
  • I would try [this](https://serverfault.com/questions/56497/how-to-modify-a-cronjob-email-subject) as this is exactly what you would like to do :) – LucioRandy Oct 08 '20 at 04:01
  • @Milag Tried that did not work. – Profer Oct 08 '20 at 05:06
  • @LucioRandy Tried that did not work. – Profer just now Edit – Profer Oct 08 '20 at 05:07
  • Hi, I think you can't change the subject of MAILTO of a cronjob. One workaround would be sending a mail manually in your .sh or in the cronjob command. Take a look at [this link](https://askubuntu.com/questions/12917/how-to-send-mail-from-the-command-line) to learn how to configure `ssmtp` or `mail`. Before you use these commands in the crontab, make sure they are working properly (try to send mails from the command line). Then you can follow link2 that @Milag posted. I tried it by myself and it worked. – lax48 Oct 09 '20 at 16:40
  • See: https://serverfault.com/questions/56497/how-to-modify-a-cronjob-email-subject – ofirule Oct 10 '20 at 18:46

3 Answers3

2

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 
Fatih Şennik
  • 1,295
  • 5
  • 12
1

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`"

SuperPoney
  • 563
  • 6
  • 21
1

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