bash - 通过 cron 执行脚本时出现“无效或不完整的多字节或宽字符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10371265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:09:23  来源:igfitidea点击:

bash - "invalid or incomplete multibyte or wide character" when executing script by cron

bashcron

提问by Daniel Gadawski

I've got a bash script sending an e-mail using mailx. Mailbox configuration is in .mailrc. I'm running it as a root:

我有一个使用mailx 发送电子邮件的bash 脚本。邮箱配置在 .mailrc 中。我以root身份运行它:

linux:~ # ./lprojekt.sh
Resolving host smtp.gmail.com . . . done.
Connecting to 173.194.70.109 . . . connected.
220 mx.google.com ESMTP n20sm30980415wiw.5
>>> EHLO linux.site
250-mx.google.com at your service, [95.49.133.188]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
>>> STARTTLS
220 2.0.0 Ready to start TLS
>>> EHLO linux.site
250-mx.google.com at your service, [95.49.133.188]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
>>> AUTH LOGIN
334 VXNlcm5hbWU6
>>> bG1mYnlkYjlAZ21haWwuY29t
334 UGFzc3dvcmQ6
>>> cHJvamVrdDEyMz==
235 2.7.0 Accepted
>>> MAIL FROM: <[email protected]>
250 2.1.0 OK n20sm30980415wiw.5
>>> RCPT TO: <[email protected]>
250 2.1.5 OK n20sm30980415wiw.5
>>> DATA
354  Go ahead n20sm30980415wiw.5
>>> .
250 2.0.0 OK 1335692819 n20sm30980415wiw.5
>>> QUIT
221 2.0.0 closing connection n20sm30980415wiw.5

As you can see, it runs correctly.

如您所见,它运行正常。

However, there's a problem when I try to run this script using cron.

但是,当我尝试使用 cron 运行此脚本时出现问题。

Here's my /etc/crontab content:

这是我的 /etc/crontab 内容:

SHELL=/bin/bash
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
HOME=/root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * *   root  test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null

* * * * * root /root/lprojekt.sh >> /root/Desktop/log.txt

Each minute (it's just for debug now), root user get a mail:

每分钟(现在只是为了调试),root 用户收到一封邮件:

Message 40:
From [email protected]  Sun Apr 29 13:50:01 2012
X-Original-To: root
Delivered-To: [email protected]
From: [email protected]
To: [email protected]
Subject: Cron <root@linux> /root/lprojekt.sh >> /root/Desktop/log.txt
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Sun, 29 Apr 2012 13:50:01 +0200 (CEST)

Invalid or incomplete multibyte or wide character
. . . message not sent.

What's the reason of this problem? How to resolve it? Thanks in advance for help.

这个问题的原因是什么?如何解决?预先感谢您的帮助。

回答by Armali

Since feature requests to mark a comment as an answerremain declined, I copy the above solution here.

由于将评论标记为答案的功能请求仍然被拒绝,我在此处复制上述解决方案。

Are you sure it's LC_TYPE and not LC_CTYPE? The latter should be the correct spelling. What's the other values of the locale variable, POSIX or pl_PL.UTF-8? You can set environment variables directly in the crontab file, just like you do with SHELL etc. Try setting LC_CTYPE and/or LC_ALL and/or LANG to pl_PL.UTF-8. Or perhaps find and remove non-ASCII characters from your script and the data files it uses. – n.m.

你确定是 LC_TYPE 而不是 LC_CTYPE?后者应该是正确的拼写。区域设置变量 POSIX 或 pl_PL.UTF-8 的其他值是什么?您可以直接在 crontab 文件中设置环境变量,就像使用 SHELL 等一样。尝试将 LC_CTYPE 和/或 LC_ALL 和/或 LANG 设置为 pl_PL.UTF-8。或者从您的脚本及其使用的数据文件中查找并删除非 ASCII 字符。– 纳米

You're right, there are LC_CTYPE instead of LC_TYPE. Other variables are POSIX. I've added to script this line: export LC_CTYPE=pl_PL.UTF-8 and it works. Thank you very much for help! – Daniel Gadawski

你是对的,有 LC_CTYPE 而不是 LC_TYPE。其他变量是 POSIX。我已将这一行添加到脚本中: export LC_CTYPE=pl_PL.UTF-8 并且它有效。非常感谢您的帮助!— 丹尼尔·加达夫斯基