2

I am migrating my python scripts from one server to a new docker container. But I am facing a strange issue with encoding, I tried several solutions without success.

If I run the script directly from the terminal (manually) it is executed with success. But if I run through crontab I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character

I created a small script just to check the encoding:

import sys
print sys.stdout.encoding

If I run manually I get the following response:

UTF-8

Then, I added this script in cron and directed the output to a file:

 * * * * *   /tmp/p.py > /tmp/p.log

It saved a empty p.log... So I think that python is not getting the encode from the crontab.

I saw a several suggestions to add in crontab the encoding variables before call the command, but it not worked for me. for exemple:

* * * * *  PYTHONIOENCODING=UTF-8 /tmp/p.py > /tmp/p.log

or

* * * * *  LANG=UTF-8 /tmp/p.py > /tmp/p.log

Also, I tried to added this variables in the head of my crontab file. No success at all.

I am using Ubuntu with few locales installed:

    locale -a 
    C 
    C.UTF-8 
    en_US.utf8 
    POSIX

And here is my locale (outside cron):

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

I have several python scripts running, so it's not a good idea to edit them (also, as I said its running perfectly when called outside cron).

1 Answer 1

7

Actually it was some problem with crontab, it wasn't reload automatically when the file was edited.

To force the reload I did:

crontab /etc/crontab

and

service cron restart

Now the crontab is loading correctly the environment variables. In the header of crontab file have the following variables:

SHELL=/bin/bash

TERM=xterm
PYTHONIOENCODING=UTF-8
LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
Sign up to request clarification or add additional context in comments.

1 Comment

Lifesaver, I've been blocked on this for hours! Thanks a lot for your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.