运行python文件规范日志
1、使用 tee
实时显示并保存日志
python test.py | tee output.log
2、只保存日志(不实时显示)
python test.py > output.log 2>&1
3、后台运行并保存日志
nohup python test.py > output.log 2>&1 &
-
nohup
:防止你关闭终端时脚本中断 -
&
:放到后台运行 -
输出保存在
output.log
4、
如果你想让每次日志文件都带时间戳区分,可以在命令行中动态生成日志名:
python test.py | tee "logs/log_$(date +%F_%H-%M-%S).log"
5、
如果你使用虚拟环境(如 (xiangxun)
),也可以这样运行:
source ~/envs/xiangxun/bin/activate
python test.py | tee output.log