7

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?

  • Set the correct environment variables in the crontab itself, /etc/environment isn't included by cron. – HBruijn Nov 5 '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 will help you setup an cron-like environment that you can work in. – Iain Nov 5 '14 at 10:03
  • Tried to add that LANG variable in to /etc/crontab - no effect – Alexander Kim Nov 5 '14 at 11:37
12

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.

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.

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
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.

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.