1

I'm trying to write a script which tests the http response from various Node servers I have running and which then emails me if any respond with a 502 error from Nginx (meaning one or more server has crashed).

My approach is to write a shell-script and have it run as a cron job, and I'm using ssmtp to send the message through Gmail. Here is a snippet:

messageTemplate=`cat /home/sites/mailmsg.txt`
...
email="$messageTemplate One or more sites is down!"
mailCommand=`mail -s info@redearmedia.ca < $email`

I'm able to get the contents of the ssmtp mailer template (To:, From:, etc.) with no issues. And I'm sure the email line concatenates just fine. The problem is that the mailCommand line fails with the line:

line 31: $email: ambiguous redirect

What is the proper way to pipe what is essentially that messageTemplate + a custom message into the first command (in this case, mail)?

  • In general if you have such problems then you should create a working command line. – Hauke Laging Feb 6 '16 at 16:12
  • You mean recreate it by running the mail command with all of the options and contents of the variable extracted? I suppose that makes good sense! – armadadrive Feb 6 '16 at 16:24
3

My crystal ball thinks that maybe your message text contains one of more of < or >.

It looks to me like your usage

mailCommand=`mail -s info@redearmedia.ca < $email`

will not do what you want: this will take the content of $email as a filename (failing somewhat because $email consists of several words), try to read its contents, pu tthose into the mail command, and then assign the output of the mail command to the variable mailCommand.

My thinking would be that you want something like

echo "$email" | mail -s info@redearmedia.ca

i.e. take the string in the variable email and feed it to the mail program?

(As a side note, many people nowadays prefer the notation $(foo) for command substitution, instead of backticks.)

  • Well explained, thank you. Your proposed solution worked. – armadadrive Feb 6 '16 at 16:31
2
messageTemplate=`cat /home/sites/mailmsg.txt`
...
mailCommand=`echo "$messageTemplate One or more sites is down"\! | mail -s info@redearmedia.ca`

or

email="$messageTemplate One or more sites is down"\!
mailCommand=`echo "$email" | mail -s info@redearmedia.ca`
  • Thank you - this could be the correct answer as it did solve my issue, but @Ulrich Schwarz did the same and gave a good explanation as to why it wasn't working as intended. +1 for proposing a working solution. – armadadrive Feb 6 '16 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.