在 Debian 上使用 nohup 运行程序后出现日志乱码,通常不是 nohup 本身的问题,而是字符编码 / 区域环境(locale)不一致导致的。常见原因和解决办法如下:
nohup 启动的程序使用的是 C / POSIX locale(默认 ASCII)结果:中文或特殊字符变成乱码
locale
如果看到:
LANG=C
LC_ALL=
就很可能会乱码。
nohup 本身不会改编码,但:
nohup xxx &LANGnohup env LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 your_command > log.out 2>&1 &
或中文环境:
nohup env LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 your_command > log.out 2>&1 &
#!/bin/bash
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
your_command >> log.out 2>&1
然后:
nohup ./run.sh &
locale -a | grep UTF-8
如果没有,安装:
sudo apt install locales
sudo dpkg-reconfigure locales
选中:
en_US.UTF-8zh_CN.UTF-8有时日志文件已被错误写入:
file log.out
如果是:
log.out: ISO-8859 text
说明程序输出时就已经不是 UTF‑8。
某些 Java / Python / Go 程序:
export PYTHONIOENCODING=utf-8
nohup python app.py > log.out 2>&1 &
nohup java -Dfile.encoding=UTF-8 -jar app.jar > log.out 2>&1 &
locale 是 UTF‑8nohup 前显式 export LANGfile 检查编码如果你愿意,可以把:
locale 输出发出来,我可以帮你精准定位。