Wednesday, October 29, 2014

cron - Unable to send external email via crontab

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?



Thanks

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...