I have a some python code that prints log messages. When run at the command line, it does fine with utf-8. Log messages that contain special characters print out fine. However, when run in the background under nohup, it barfs on utf-8 characters.
nohup python2.7 myProgram.py &
The error I see is the usual "try to encode utf in ascii" error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 71: ordinal not in range(128)
I assume this is because nohup signals to python that it doesn't have a normal terminal, so it defaults to ascii. Is there any way to either tell nohup to run with utf-8 enabled or to set this up so that utf-8 characters won't cause a crash when running under nohup in the background?
Use PYTHONIOENCODING:
export PYTHONIOENCODING=utf-8
nohup python2.7 myProgram.py &
For example, if
myProgram.py:
unicode_obj=u'\N{INFINITY}'
print(unicode_obj)
then running
nohup python2.7 myProgram.py > /tmp/test &
produces
/tmp/test:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u221e' in position 0: ordinal not in range(128)
while
export PYTHONIOENCODING=utf-8
nohup python2.7 myProgram.py > /tmp/test &
produces
/tmp/test:
∞
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With