Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linux - Use crontab job send mail, The email text turns to an attached file which named ATT00001.bin

I want to analysis some data in one linux server,then send the it as Email text to my Email account , But when i execute this shell scripts in shell command, It works well, Weird is that when i put all the procedure into crontab job, The Email text will turns to an attached file, Can someone help?

#* * * * * sh -x /opt/bin/exec.sh >> /opt/bin/mailerror 2>&1

/* exec.sh */
#/bin/sh
cd /opt/bin
./analysis.sh > test
mail -s "Today's Weather" [email protected] < test

But when i execute exec.sh in shell command line directly, The Email will get text, Can someone explain it for me, grate thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ran into the same problem myself, only I'm piping text output into mailx - Heirloom mailx 12.4 7/29/08

When running the script on the command line the email came out as normal email with a text body.
However, when I ran the exact same script via crontab the body of the email came as an attachment - ATT00001.BIN (Outlook), application/octet-stream (mutt) or "noname" (Gmail).

Took some research to figure this out, but here goes:

Problem

Mailx will, if it encounters unknown / control characters in text input, convert it into an attachment with application/octet-stream mime-type set.

From the man page:

for any file that contains formatting characters other than newlines and horizontal tabulators

So you need to remove those control characters, which can be done with i.e. tr

echo "$Output" | /usr/bin/tr -cd '11121540-176' | mail ...

However since I had Norwegian UTF8 characters: ??? - the list expand, and you don't really want to maintain such a list, and I need the norwegian characters.

And inspecting the attachment I found I had only , the "regular" ASCII characters in range 32-176 - all printable and 184 and 195 --> UTF8

Sollution

Explicitly set the locale in your script:

LANG="en_US.UTF8" ; export LANG

Run export in your shell - or setenv if you run csh or tcsh to determine what your locale is set to.

Explanation

Mailx - when run in your shell - with LANG set to .UTF8, will correctly identify the UTF8 chars and continue.

When run in crontab LANG is not set, and default to LANG=C, since by default crontab will run only a restricted set of environment variables (system dependant).

mailx (or other programs) will then not recognize UTF8 characters and determine that the input containes unknown control characters.

My issue was UTF8 characters, yours could be other control characters in your input. Run it through hexdump or od -c, but since it works OK in a regular shell I'm suspecting LANG issues.

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...