Nohup is not writing log to output file

强颜欢笑 提交于 2019-11-28 02:54:32

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.

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

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

Using '-u' with 'nohup' worked for me. Everything will be saved in "nohup.out " file. Like this-

nohup python -u code.py &
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

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)

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!