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