程序后台运行并实时输出日志
Swift Lv6

将程序非挂断放在后台执行,命令如下:

1
nohup python -u main.py > run.log 2>&1 &

这里是将程序的标准输出和标准错误都重定向到了 run.log 文件中。

需要注意的是,python程序的输出有缓冲,不会立刻写入到日志文件中,使用 -u 参数来解决此问题:

Force the binary I/O layers of stdout and stderr to be unbuffered. stdin is always buffered. The text I/O layer will still be line-buffered.

程序放到后台执行了,每次手动查看日志很麻烦,使用 tail 命令来实时查看日志:

1
tail -f run.log


参考

Powered by Hexo & Theme Keep
Unique Visitor Page View