nohup 日志出现乱码,通常不是 nohup 本身的问题,而是字符编码不一致导致的。下面按常见场景给你排查和解决思路。
locale
重点关注:
LANG=zh_CN.UTF-8
LC_CTYPE=zh_CN.UTF-8
如果看到:
LANG=CLANG=POSIXUTF-8✅ 说明系统编码不是 UTF-8,很可能是乱码根源。
nohup java -Dfile.encoding=UTF-8 -jar app.jar > nohup.out 2>&1 &
或 Python:
nohup python3 -X utf8 app.py > nohup.out 2>&1 &
✅ 适用于 Java / Python / 自定义程序
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
nohup your_command > nohup.out 2>&1 &
⚠️ 注意:
~/.bashrc 可永久生效例如原编码是 GBK:
iconv -f gbk -t utf-8 nohup.out > nohup_utf8.out
常见编码猜测:
GBKUTF-8less nohup.out
# 如果乱码
export LANG=zh_CN.UTF-8
less nohup.out
或 vim:
vim nohup.out
:set fileencoding
nohup java -Dfile.encoding=UTF-8 -jar app.jar > nohup.out 2>&1 &
nohup python3 -X utf8 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
locale -a | grep utf8
如果没有,安装(示例):
yum install -y langpacks-zh_CN
# 或
apt install -y locales
dpkg-reconfigure locales
locale 是否为 UTF-8export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
nohup ./your_program > nohup.out 2>&1 &
如果你愿意,可以把:
locale 输出贴出来,我可以帮你精准定位是哪一种乱码。