简体   繁体   中英

Email not send in the cron job shell script

I have this cron job entry:

0 0 * * * /application-data/sendUrl.sh

/application-data/sendUrl.sh has this code:

auditFile="/application-data/auditUrl.txt"
[ -e $auditFile ] && (echo -e "Subject: Foo\nTo: user@example.com\n\n" `cat $auditFile` | sendmail -t ; rm -f $auditFile )

The shell script has all root privileges and correct file permissions. Running it from the command line it sends the email. Only when it's executed by the cron job the email is not sent, but the file at the end of the command list is deleted so I know the shell script has been executed.

Any idea what am I doing wrong so the email is not sent when running as cron job?

Your script doesn't have a shebang so it'll be executed with sh ; echo -e behavior is implementation defined with sh

Also, you're deleting the file even if sendmail fails; you should at least test the return status before doing the deletion.

Does it work better like this?

#!/bin/sh

auditFile="/application-data/auditUrl.txt"

[ -f "$auditFile" ] || exit

printf 'Subject: %s\nTo: %s\n\n' "Foo" "user@example.com" |
cat - "$auditFile" |
sendmail -t &&
rm -f "$auditFile"

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