Ubuntu – sendmail with cron job

cronsendmail

I've written a script and everything is working so well according to my needs:

a=$(df -h /mnt/smb | tail -n +3 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)
echo $a

if [[ $a -ge 30 ]]; then
    cat /etc/diskspace/text.txt | sendmail test.gmail.com
fi

I want to run this script every minute via Cron, see my crontab:

* * * * * /etc/diskspacae/vpndrive.sh

As you can see, after running this script if drive space is more than 80%, I want to use sendmail to get a notification in my email.

But I haven't got it when I've run this script from Cron, and I've got this error message in /var/spool/mail/root.

ERROR Message: /etc/diskspace/vpndrive.sh line 34 : sendmail: command not found. 

Best Answer

Don't forget that script executed by Cron are ran in an environment with a limited PATH variable. This means that on the console when you test your script, the console shell will find sendmail without problem. But when ran from Cron, sendmail is not found.

Best practise when writing scripts for Cron is to put the full path to access the command you want to run, just to be sure.

Usually, the sendmail binary is install under /usr/sbin, a path which is not part of the directories looked up by Cron by default. So I would advise to :

  1. Look for the place of sendmail binary (locate sendmail or find / -type f -name sendmail) if locate is not installed)
  2. Write down the full path to execute sendmail in your script
Related Question