解决Linux中使用nohup执行Python脚本没有将stdout和stderr输出重定向到log文件中的问题
# 问题描述
bash终端中运行如下命令,会发现nohup.log文件中并没有写入Python程序的stdout和stderr
nohup python3 test_script.py >> nohup.log 2>&1 &
1
# 解决办法
python3
后边跟上一个-u
参数就可以了
nohup python3 -u test_script.py >> nohup.log 2>&1 &
1
# 原因
使用nohup
执行Python
程序,默认stdout
和stderr
都被缓冲了。
以下是-u
参数的官方解释
-u : force the binary I/O layers of stdout and stderr to be unbuffered;
stdin is always buffered; text I/O layer will be line-buffered;
also PYTHONUNBUFFERED=x
-u:强制stdout和stderr的二进制I/O层不被缓冲;
stdin总是被缓冲;文本I/O层将被行缓冲;
也PYTHONUNBUFFERED = x
1
2
3
4
5
6
2
3
4
5
6
编辑 (opens new window)
上次更新: 2021/08/10, 00:23:06
- 01
- Python实现对字符串的加解密02-25
- 02
- Python3对大文件中指定字符进行排序再写入到新的文件10-24
- 03
- Ubuntu下配置adb环境连接Android设备进行调试08-17