nohup 本身不会造成乱码,乱码通常来自 字符编码不一致 或 终端/重定向环境不同。下面按常见原因和解决办法说明。
例如:
结果:nohup.out 打开是乱码
✅ 解决:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
再启动程序:
nohup your_app > nohup.out 2>&1 &
nohup java -Dfile.encoding=UTF-8 -jar app.jar > nohup.out 2>&1 &
nohup python3 -u app.py > nohup.out 2>&1 &
确保源码文件是 UTF-8
cat / vim 打开时终端编码不对✅ 检查终端编码:
locale
✅ 用 less 或指定编码打开:
less nohup.out
或
iconv -f GBK -t UTF-8 nohup.out
nohup ./your_program \
> app.log 2>&1 &
并设置:
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
iconv -f GBK -t UTF-8 nohup.out > nohup_utf8.out
iconv -f UTF-8 -t GBK nohup.out > nohup_gbk.out
⚠️ 如果已经“双重编码”,可能无法完全恢复。
✅ 不要直接看 nohup.out
✅ 自己指定日志文件
✅ 统一 UTF-8
✅ 程序内部显式设置编码
如果你愿意,可以告诉我:
我可以给你精确的一行命令解决方案。