简体   繁体   English

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

[英]Nohup is not writing log to output file

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.但似乎 nohup 没有向日志文件写入任何内容。 cmd.log is created but is always empty. cmd.log 已创建但始终为空。 In the python script, I am using sys.stdout.write instead of print to print to standard output.在 python 脚本中,我使用sys.stdout.write而不是print来打印到标准输出。 Am I doing anything wrong?我做错了什么吗?

您可以使用-u标志运行 Python 以避免输出缓冲:

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

It looks like you need to flush stdout periodically (eg sys.stdout.flush() ).看起来您需要定期刷新标准输出(例如sys.stdout.flush() )。 In my testing Python doesn't automatically do this even with print until the program exits.在我的测试中,即使使用print Python 也不会自动执行此操作,直到程序退出。

  • Using -u with nohup worked for me.-unohup一起使用对我nohup Using -u will force the stdout , stderr streams to be unbuffered.使用-u将强制stdoutstderr流无缓冲。 It will not affect stdin.它不会影响标准输入。 Everything will be saved in " nohup.out " file.一切都将保存在“ nohup.out ”文件中。 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 .此外,您可以使用PYTHONUNBUFFERED If you set it to a non-empty string it will work same as the -u option.如果将其设置为非空字符串,它将与-u选项相同。 For using this run below commands before running python code.在运行 python 代码之前使用这个运行下面的命令。

     export PYTHONUNBUFFERED=1

    or要么

    export PYTHONUNBUFFERED=TRUE

PS- I will suggest using tools like cron-job to run things in the background and scheduled execution. PS-我会建议使用像 cron-job 这样的工具在后台运行和计划执行。

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 https://docs.python.org/2/using/cmdline.html#cmdoption-u

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)

I had a similar issue, but not connected with a Python process.我有一个类似的问题,但与 Python 进程无关。 I was running a script which did a nohup and the script ran periodically via cron.我正在运行一个执行 nohup 的脚本,并且该脚本通过 cron 定期运行。

I was able to resolve the problem by:我能够通过以下方式解决问题:

  1. redirecting the stdin , stdout and stderr重定向 stdin 、 stdout 和 stderr
  2. ensuring the the script being invoked via nohup didn't run anything else in the background确保通过 nohup 调用的脚本没有在后台运行任何其他内容

PS: my scripts were written in ksh running on RHEL PS:我的脚本是用在 RHEL 上运行的 ksh 编写的

I run my scripts in the following way and I have no problem at all:我按以下方式运行我的脚本,我完全没有问题:

nohup python my_script.py &> my_script.out &

comparing with your syntax looks like you are only missing a "&" symbol after your input...与您的语法相比,您输入后似乎只缺少一个“&”符号...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM