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: [email protected]\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?
CodePudding user response:
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" "[email protected]" |
cat - "$auditFile" |
sendmail -t &&
rm -f "$auditFile"