简体   繁体   中英

sendmail not working in crontab

I am facing issue when I try to execute my script from crontab but when I try to execute manually, it works perfectly fine.

Crontab File

54 * * * * /opt/SP/home/osbadm/scripts/tmp1.sh 2>&1 

Sendmail function

SUBJECT="test"
TO="to@gmail.com" (
echo "TO: $TO"
echo "MIME-Version: 1.0"
echo "Subject: $SUBJECT"
echo "Content-Type: text/html"
cat $EXTRACT_CST_HTML
) | /usr/sbin/sendmail -f from@gmail.com $TO

Can anyone please help me out.

You can't put a variable assignment before the ( that starts a subshell. If you'd pasted your script into shellcheck.net it would have told you:

SC1036: '(' is invalid here. Did you forget to escape it?

Also, even if you could, it would only set the variable inside the subprocess's environment, it wouldn't be visible when processing the arguments to sendmail . So put that assignment on its own line. Also, a here-doc is an easier way to pass multiple lines of input to sendmail .

TO="to@gmail.com"
(
echo "TO: $TO"
echo "MIME-Version: 1.0"
echo "Subject: $SUBJECT"
echo "Content-Type: text/html"
cat $EXTRACT_CST_HTML
) | /usr/sbin/sendmail -f from@gmail.com $TO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM