8

I have following command in a cronjob:

*/5 * * * * php /var/www/domain/yii rss/parse

It outputs email in a wrong charset:

Content-Type: text/plain; charset=ANSI_X3.4-1968

But when i launch that command directly in CLI and output it to a log:

php /var/www/domain/yii rss/parse > log

I get the right encoding - UTF-8

Already tried to set lang in /etc/environment:

LANG=en_US.UTF-8

Restarted the cron, but still it uses ANSI via CRON. Any ideas?

Alexander Kim
  • 597
  • 3
  • 8
  • 21
  • Set the correct environment variables in the crontab itself, `/etc/environment` isn't included by cron. – HBruijn Nov 05 '14 at 10:00
  • I don't think we're going to be able answer this directly, you'll have to work out what is missing from the cron environment yourself and add it. [This answer](http://serverfault.com/a/541368/9517) will help you setup an cron-like environment that you can work in. – user9517 Nov 05 '14 at 10:03
  • Tried to add that LANG variable in to /etc/crontab - no effect – Alexander Kim Nov 05 '14 at 11:37

4 Answers4

13

Solved my issue by adding in to a crontab:

crontab -e

At the top of file i wrote:

CONTENT_TYPE="text/plain; charset=utf-8"

Now all of my cron job email's are in UTF-8 Charset.

Alexander Kim
  • 597
  • 3
  • 8
  • 21
  • 1
    Thanks for posting your solution! I've also had this problem. BTW, it's OK to mark your own answer as accepted. This way you'll make it even more helpful by the clear indication that this is a solution. Thanks! – imz -- Ivan Zakharyaschev May 20 '15 at 09:12
  • Though I want to note that the comments at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=410057 , https://bugs.launchpad.net/ubuntu/+source/cron/+bug/1321227 , https://bugs.launchpad.net/ubuntu/+source/cron/+bug/140896 suggest that it would be enough to use correct locale variables (LANG and LC_*) in the cron's environment. But you wrote this didn't work for you. Anyway, the solution with `CONTENT_TYPE` works definitely. – imz -- Ivan Zakharyaschev May 20 '15 at 11:36
2

Referring to this problem I found in Debian Jessie in /etc/default/cron the following:

# Whether to read the system's default environment files (if present)
# If set to "yes", cron will set a proper mail charset from the
# locale information. If set to something other than 'yes', the default
# charset 'C' (canonical name: ANSI_X3.4-1968) will be used.
#
# This has no effect on tasks running under cron; their environment can
# only be changed via PAM or from within the crontab; see crontab(5).
READ_ENV="yes"

In other words the default is that in this distribution it reads the environment files. After rerunning dpkg-reconfigure locales (in my case the default was already set to UTF8) I looked at /etc/environment and discovered that it was empty. Once I inserted LC_ALL=en_US.UTF-8 there, the cron job emails were with correct character set header.

ñull
  • 121
  • 3
1

When you give the command by CLI you got the utf-8 chatset, i think, because you using a MAC OS PC or Linux PC

I told this because the current LANG of your PC begin copied in your ssh session

grep -i LANG /etc/ssh/sshd_config 
AcceptEnv LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES

from man sshd_config

AcceptEnv
             Specifies what environment variables sent by the client will be copied into the session’s environ(7).  See SendEnv in ssh_config(5) for how to
             configure the client.  Note that environment passing is only supported for protocol 2.  Variables are specified by name, which may contain the
             wildcard characters ‘*’ and ‘?’.  Multiple environment variables may be separated by whitespace or spread across multiple AcceptEnv directives.
             Be warned that some environment variables could be used to bypass restricted user environments.  For this reason, care should be taken in the
             use of this directive.  The default is not to accept any environment variables.

Your crond process are using charset=ANSI_X3.4-1968, maybe this is the default system LANG, but if want to change this

 man 5 crontab
c4f4t0r
  • 5,149
  • 3
  • 28
  • 41
1

I had to solve this issue globally for all users and not specific one. I've tried to set up /etc/environment and /etc/default/locale then restart cron. This didn't help. Right answer for me was using env command in upstart script(i am running ubuntu server):

env LC_ALL=en_US.UTF-8

cat /etc/init/cron.conf 
# cron - regular background program processing daemon
#
# cron is a standard UNIX program that runs user-specified programs at
# periodic scheduled times

description     "regular background program processing daemon"

start on runlevel [2345]
stop on runlevel [!2345]

expect fork
respawn
env LC_ALL=en_US.UTF-8

exec cron

Then i've restarted cron and got correct mail in utf-8.

Navern
  • 1,569
  • 1
  • 9
  • 14