nohup 本身不会制造乱码,乱码通常是以下原因之一造成的:
下面按最常见场景给你解决方案。
file nohup.out
常见结果:
UTF-8 text → 文件没问题,是查看方式问题ISO-8859 text → 程序输出不是 UTF-8data → 二进制混进日志了nohup java -Dfile.encoding=UTF-8 -jar app.jar > nohup.out 2>&1 &
nohup python -u app.py > nohup.out 2>&1 &
或在代码里:
import sys
sys.stdout.reconfigure(encoding='utf-8')
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
nohup env LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 your_command > nohup.out 2>&1 &
cat nohup.out
less nohup.out
确保终端是 UTF-8(如 Xshell / SecureCRT / Terminal)
如果是 GBK:
iconv -f GBK -t UTF-8 nohup.out > nohup_utf8.out
如果是 ISO-8859-1:
iconv -f ISO-8859-1 -t UTF-8 nohup.out > nohup_utf8.out
nohup your_command \
> logs/app.log 2>&1 &
并加:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
如果你愿意,可以:
我可以帮你精准定位是哪一步出的问题。