我有一个简单的 python 脚本,它只运行一个无限的 while 循环并打印“正在运行”。当我在后台使用 nohup 启动它时,我在当前目录中看到一个 nohup.out 文件,但没有任何内容写入它。我很困惑。我尝试了以下方法
nohup test.py
写入nohup.out但显然不在后台运行
nohup test.py &
在后台运行但不写入 nohup.out
nohup test.py 2>&1 &
也在后台运行但不写入 nohup.out
我打赌我错过了一些简单的东西。有什么想法吗?
这是我的python脚本供引用:
import sys
from time import sleep
def main():
print "Starting...."
while True:
print "running..."
sleep(5.00)
if __name__ == "__main__":
main()
最佳答案
也许你的标准输出没有立即刷新,因为它没有连接到终端。尝试使用
import sys
sys.stdout.flush()
刷新你的输出。
在 python3 中,您可以使用 flush=True
作为 print 函数的参数来执行此操作。
print("running...", flush=True)
关于ubuntu - 为什么我的 nohup.out 是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537932/