I'm trying to send email out from crontab. I've tried making crontab run a basic shell script, as well as specifying the actual command within the crontab. I've tried doing this with mpack and ssmtp . I've noticed that if i execute the command or shell script in a terminal, it works fine. It only fails when i try to schedule it.

This is the basic essence of the command i need to run, where im emailing abc@abc.com the contents of a file. The file is generated daily and is named after the year, month, and day.

echo -e "to: abc@abc.com\nsubject: abc123\n" | ssmtp abc@abc.com < `date +%y%m%d`.txt

Similar thing with mpack

mpack -s "abc123" `date +%y%m%d`.txt abc@abc.com

I've figured that it has something to do with the date variable. If i substitute that with the actual name of the file, then it all works fine. I've made sure to escape the % symbol, and have tried replacing the backtick with $(date +%y%m%d) with no luck.

Crontab looks like this

10 10 * * * /home/user/./script.sh

Also tried this method

10 10 * * * echo -e "to: abc@abc.com\nsubject: abc123\n" | ssmtp abc@abc.com < `date +\%y\%m\%d`.txt

I've made sure that the shell script includes #!/bin/sh, checked all file permissions, and changed the environmental path to include the directories for ssmtp and mpack.

Any suggestions why the date variable is making this fail? Do i need to escape anything else?



I think you should specify full path to binaries and file.

Your script.sh should look like this :

/path/to/echo -e "to: abc@abc.com\nsubject: abc123\n" | /path/to/ssmtp abc@abc.com < /path/to/`date +%y%m%d`.txt

Then your Crontab should be like this :

10 10 * * * /home/user/script.sh

(assuming your script has the x bit chmod +x)

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.