I have the following bash script...

# Delete JUNK temp folder 

echo "Cleansed JUNK directory successfully!" 
/usr/bin/mail -s "JUNK folder deleted successfully" example@gmail.com <<< "Success!" 
/usr/bin/mail -s "JUNK folder not found" example@gmail.com <<< "Success!" 

I have it in my crontab as below

* * * * * /bin/sh /home1/ramnath/public_html/myscripts/deltemp.sh

Q1: Why is the script is not running? Is there an error? When I manually run the script in the terminal, I get an error:

bash myscripts/deltemp.sh Cleansed JUNK directory successfully! myscripts/deltemp.sh: line 8: mail: command not found


NOTE: The issue is fixed. I just used mailx -s "JUNK directory deleted" "example@gmail.com"

  • The mail command is not found in your current PATH. Either specify the absolure filename like /bin/mail or change the PATH variable. – ott-- Jan 22 '16 at 12:16

You will have to give absolute paths in your script, cron might run your script somewhere else, not in your $HOME.

You probably haven't got the venerable mail(1) program installed. And if you have, for sanity's sake, also give full paths to any executables your script runs.

In general, for any script that runs with extra privileges (as another user, started automatically) it is a little extra work to use absolute paths, set PATH explicitly, or run executables by full path. That way you protect against running in some strange directory, or picking up the wrong (even malicious) executable, and save much grief.

  • I have provided the complete path in my script. Is that what you meant? – Ramnath Jan 22 '16 at 12:38
  • JUNK/MAIL/FOLDER is not a "complete path", far from it. – vonbrand Jan 22 '16 at 12:53
  • In my server I have specified the path as /home1/ramnath/public_html/../junk/folder/ Since this a public forum I am abstaining from disclosing the actual path. – Ramnath Jan 22 '16 at 13:02
  • @Ramnathm, then use a fake path like /home/joesixpack/here/is/junk – vonbrand Jan 22 '16 at 13:08
  • SOLVED. The only error that I fixed was use mailx instead. mailx -s "JUNK directory deleted" "example@gmail.com" – Ramnath Jan 22 '16 at 13:15

1) The error pretty much speaks for itself: your script cannot find the mail binary. Either it isn't installed, or it is but isn't in your $PATH and therefore you have to call it specifying the full path (e.g. /usr/bin/mail).

2) You do not need to attach a timestamp manually to the payload passed to mail, as e-mail messages bear already a timestamp in the Date: header:

Date: Fri, 22 Jan 2016 06:30:10 -0800

Just ensure that the sending server is time-synchronized (e.g. via NTP) and you'll be fine.

  • mailx -s "JUNK directory deleted" "example@gmail.com" solved the issue. Not sure why my question was awarded -1 by someone?! – Ramnath Jan 22 '16 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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