在 CentOS 上,nohup 本身并不决定日志的编码,nohup.out 的编码取决于:
程序本身输出到 stdout/stderr 的字符编码
nohup 只是:
SIGHUP所以:
nohup java -jar app.jar > nohup.out 2>&1 &
nohup.out 的编码 = 程序输出的编码
echo $LANG
# zh_CN.UTF-8 或 en_US.UTF-8
✅ nohup.out 通常是 UTF-8
如果程序内部使用 GBK(如:
new String(bytes, "GBK")
或日志框架配置错误)
✅ nohup.out 就是 GBK
file nohup.out
可能结果:
nohup.out: UTF-8 Unicode text
nohup.out: ISO-8859 text
nohup.out: ASCII text
iconv -f UTF-8 -t UTF-8 nohup.out > /dev/null
cat nohup.out
nohup java -Dfile.encoding=UTF-8 -jar app.jar > nohup.out 2>&1 &
✅ 最推荐
iconv -f GBK -t UTF-8 nohup.out > nohup_utf8.out
nohup java -jar app.jar \
> >(iconv -f GBK -t UTF-8 > app.log) 2>&1 &
export LANG=zh_CN.UTF-8
nohup your_command &
⚠️ 只对部分程序有效
CentOS 上 nohup 日志没有固定编码,取决于程序输出编码,默认通常是 UTF-8。
✅ 查看编码用 file
✅ Java 程序用 -Dfile.encoding=UTF-8
✅ 乱码就 iconv 转
如果你愿意,可以告诉我:
我可以给你一个完全对症的解决方案。