1

This is the code I have in a bash script that runs as a cronjob. The cronjob run as root.

   /usr/sbin/sendmail [email protected]<<EOF
   subject:Backup Error!
   from:backup@server01
   $error
        EOF

There is code after this and the email I get is as follows:

From the root user on the machine.

and the message includes:

   subject:Backup Error!
   from:backup@server01
   $error
        EOF
   More code...
   that is in the script
   all the way to the end...

I have tried other variations, this is the closest I've got. I tried this in a regular script and it worked properly. Whats going on, and how can I send this email, specifying the subject and form sender?

3 Answers 3

2

The previous answers have already mentioned:

  • bad end of "here document" (END)
  • missing empty line between headers and body

Additional fixes:

  • -i command line option to stop special treatment for lines starting with dot
  • -- to separate command line options from addresses of recipients (good style recomendation)

Corrected script:

/usr/sbin/sendmail -i -- [email protected] <<EOF
subject: Backup Error!
from:backup@server01

$error
EOF
1

I suggest you to not use any whitespace character in front or after the second limit string EOF. If you use just only one space in front or after the limit string, that special-purpose code block will never work. So, use only:

EOF

And you should read some bash scripting guides in this sense like Chapter 19. Here Documents.

1

Sendmail (and email in general, it seems) requires a blank line between headers and body. The following example worked.

$ sendmail [email protected] <<EOF
> subject: testing
> from: [email protected]
> 
> This is my body!
> EOF

(Obviously pasting that in with the >s in there won't work, just alter what you're doing so that there's a blank line between your headers and error message).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .