0

I wrote the simple backup bash script and at the end of it, it's sending an email to me that backup is ready. Everything works perfect when I run this script from terminal (root), but when the script is running by CRON, email is not sending :-/.

#!/bin/sh
filename=$(date +%d-%m-%Y)
backup_dir="/mnt/backup/"
email_from_name="BACKUP"
email_to="my@email"
email_subject="Backup is ready"
email_body_file="/tmp/backup-email-body.txt"

tar czf "$backup_dir$filename.tgz" "/home/www"

echo "Subject: $email_subject" > $email_body_file
ls $backup_dir -sh >> $email_body_file

sendmail -F $email_from_name  -t $email_to < $email_body_file
badam
  • 101
  • 1
  • 1
  • 1
    -t means extract recipients from message headers. -F sets the fullname of the sender and -f sets the envelope sender address. – adamo Jul 06 '12 at 20:54

2 Answers2

2

Cronjobs run in a minimal environment, so you can't even assume that $PATH is set correctly. The script is probably failing to find the sendmail executable. Set $PATH explicitly in the crontab to something like this, or specify the full path to sendmail in the script.

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
mgorven
  • 30,615
  • 7
  • 79
  • 122
1

This is the environment issue , we need to set the env path in cron as below

38 03 * * 5  /bin/ksh c ". /home/adcis/caliber/.profile ;
/home/xyz/abc/ABCD_BATCH/REPORT_ABC/amb_main_script.ksh" >> /home/xyz/abc/ABCD_BATCH/LOGS/output.txt
squillman
  • 37,883
  • 12
  • 92
  • 146
user226917
  • 11
  • 1