This question already has an answer here:

I have a shell script which sends email when the disk is more than 80% full; when I run the script as sh -x script.sh or ./script.sh I get an email but when I keep the script in cronjob I do not get any email.

The script has execute permissions (755).

When I type mail I can see the cronjob has ran at that time, but there is no email sent.

marked as duplicate by yaegashi, G-Man, cuonglm, Anthon, eyoung100 Aug 6 '15 at 3:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • HINT: Set the PATH variable explicitly in your script and make sure it is exported. – mdpc Aug 5 '15 at 6:02
  • @mdpc , I am new to linux, shell scripting. Do I need to add the line export PATH=$PATH:/path/to/dir1 where /path/to/dir is the script.sh path ? – YRVK Aug 5 '15 at 6:15
  • When I run $PATH in my server I can see some values already set, If I add this PATH variable in my script (which points to my script.sh path) will it overwrite the previous values ? – YRVK Aug 5 '15 at 6:20
  • @mdpc I moved the shell script to one of the directories as listed in the $PATH, now the script runs fine from crontab, thank you for the hint – YRVK Aug 5 '15 at 6:52
  • @YRVK. Please Choose "Answer my own Question" below your name, as this is a duplicate and I'm voting as such – eyoung100 Aug 6 '15 at 3:50

You can do 2 things.

  1. Put the full path in for sendmail. /usr/sbin/sendmail for example.
  2. Put the following commands at the beginning of you crontab:

SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin

Not the answer you're looking for? Browse other questions tagged or ask your own question.