Python Nohup 没有将日志写入输出文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12919980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nohup is not writing log to output file
提问by
I am using the following command to run a python script in the background:
我正在使用以下命令在后台运行 python 脚本:
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.writeinstead of printto print to standard output. Am I doing anything wrong?
但似乎 nohup 没有向日志文件写入任何内容。cmd.log 已创建但始终为空。在 python 脚本中,我使用sys.stdout.write而不是print打印到标准输出。我做错了什么吗?
采纳答案by wulong
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 printuntil the program exits.
看起来您需要定期刷新标准输出(例如sys.stdout.flush())。在我的测试中,即使print在程序退出之前,Python 也不会自动执行此操作。
回答by vz0
You can run Python with the -uflag to avoid output buffering:
您可以使用该-u标志运行 Python以避免输出缓冲:
nohup python -u ./cmd.py > cmd.log &
回答by Ryu_hayabusa
export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &
or
或者
nohup python -u ./cmd.py > cmd.log &
回答by Nurul Akter Towhid
Using
-uwithnohupworked for me. Using-uwill force thestdout,stderrstreams 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-uoption. For using this run below commands before running python code.export PYTHONUNBUFFERED=1or
export PYTHONUNBUFFERED=TRUE
使用
-uwithnohup对我有用。使用-u将强制stdout,stderr流无缓冲。它不会影响标准输入。一切都将保存在“ 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
P.S.-I will suggest using tools like cron-job to run things in the background and scheduled execution.
PS-我会建议使用像 cron-job 这样的工具在后台运行和计划执行。
回答by Ganesh Krishnan
Python 3.3 and above has a flush argument to print and this is the only method that worked for me.
Python 3.3 及更高版本有一个用于打印的刷新参数,这是唯一对我有用的方法。
print("number to train = " + str(num_train), flush=True)
print("Using {} evaluation batches".format(num_evals), flush=True)
回答by Pradeep Anchan
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.
我有一个类似的问题,但与 Python 进程无关。我正在运行一个执行 nohup 的脚本,并且该脚本通过 cron 定期运行。
I was able to resolve the problem by:
我能够通过以下方式解决问题:
- redirecting the stdin , stdout and stderr
- ensuring the the script being invoked via nohup didn't run anything else in the background
- 重定向 stdin 、 stdout 和 stderr
- 确保通过 nohup 调用的脚本没有在后台运行任何其他内容
PS: my scripts were written in ksh running on RHEL
PS:我的脚本是用在 RHEL 上运行的 ksh 编写的

