运行python脚本后台执行

在Linux中,可以使用nohup将脚本放置后台运行,如下:

nohup python myscript.py params1 > nohup.out 2>&1 & 
  • 1

但直接使用上面代码,无法在程序运行过程中查看Python中的print "computing" 输出结果,比如在每次循环中使用print语句等。原因是python的输出有缓冲,导致nohup.out不能够马上看到输出。

解决方法:

  • 使用-u参数,使得python不启用缓冲。

修改命令如下:

nohup python -u myscript.py params1 > nohup.out 2>&1 & 
  • 如果没有参数
nohup python myscript.py > nohup.out 2>&1 & 

 

你可能感兴趣的:(运行python脚本后台执行)