问题:Nohup没有将日志写入输出文件

我正在使用以下命令在后台运行python脚本:

nohup ./cmd.py > cmd.log &

但是似乎nohup没有向日志文件写入任何内容。cmd.log已创建,但始终为空。在python脚本中,我使用sys.stdout.write而不是print打印到标准输出。我做错什么了吗?

I am using the following command to run a python script in the background:

nohup ./cmd.py > cmd.log &

But it appears that nohup is not writing anything to the log file. cmd.log is created but is always empty. In the python script, I am using sys.stdout.write instead of print to print to standard output. Am I doing anything wrong?


回答 0

看来您需要定期刷新标准输出(例如sys.stdout.flush())。在我的测试中,Python不会自动执行此操作,print直到程序退出。

It looks like you need to flush stdout periodically (e.g. sys.stdout.flush()). In my testing Python doesn’t automatically do this even with print until the program exits.


回答 1

您可以使用带有-u标志的Python 来避免输出缓冲:

nohup python -u ./cmd.py > cmd.log &

You can run Python with the -u flag to avoid output buffering:

nohup python -u ./cmd.py > cmd.log &

回答 2

  • 使用-u with nohup对我有用。使用-u将迫使stdoutstderr流是缓冲。它不会影响标准输入。一切都将保存在“ nohup.out ”文件中。像这样-

    nohup python -u your_code.py &

    您也可以将其保存到目录中。这条路-

    nohup python -u your_code.py > your_directory/nohup.out &
  • 另外,您可以使用PYTHONUNBUFFERED。如果将其设置为非空字符串,它将与该-u选项相同。要在运行python代码之前使用以下命令运行此命令。

    export PYTHONUNBUFFERED=1

    要么

    export PYTHONUNBUFFERED=TRUE

PS: 我建议使用cron-job之类的工具在后台运行程序并安排执行时间。

  • Using -u with nohup worked for me. Using -u will force the stdout, stderr streams to be unbuffered. It will not affect stdin. Everything will be saved in “nohup.out ” file. Like this-

    nohup python -u your_code.py &
    

    You can also save it into your directory. This way-

    nohup python -u your_code.py > your_directory/nohup.out &
    
  • Also, you can use PYTHONUNBUFFERED. If you set it to a non-empty string it will work same as the -u option. For using this run below commands before running python code.

    export PYTHONUNBUFFERED=1
    

    or

    export PYTHONUNBUFFERED=TRUE
    

P.S.- I will suggest using tools like cron-job to run things in the background and scheduled execution.


回答 3

export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &

要么

nohup python -u ./cmd.py > cmd.log &

https://docs.python.org/2/using/cmdline.html#cmdoption-u

export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &

or

nohup python -u ./cmd.py > cmd.log &

https://docs.python.org/2/using/cmdline.html#cmdoption-u


回答 4

Python 3.3及更高版本有一个flush参数用于打印,这是唯一对我有用的方法。

print("number to train = " + str(num_train), flush=True)
print("Using {} evaluation batches".format(num_evals), flush=True)

Python 3.3 and above has a flush argument to print and this is the only method that worked for me.

print("number to train = " + str(num_train), flush=True)
print("Using {} evaluation batches".format(num_evals), flush=True)

回答 5

我有一个类似的问题,但未与Python进程关联。我正在运行一个脚本,该脚本执行了nohup,并且该脚本通过cron定期运行。

我可以通过以下方法解决问题:

  1. 重定向标准输入,标准输出和标准错误
  2. 确保通过nohup调用的脚本没有在后台运行其他任何东西

PS:我的脚本是用RHEL上运行的ksh编写的

I had a similar issue, but not connected with a Python process. I was running a script which did a nohup and the script ran periodically via cron.

I was able to resolve the problem by:

  1. redirecting the stdin , stdout and stderr
  2. ensuring the the script being invoked via nohup didn’t run anything else in the background

PS: my scripts were written in ksh running on RHEL


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。