Nohup is not writing log to output file

2020-01-30 02:51发布

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?

标签: python nohup
6条回答
Fickle 薄情
2楼-- · 2020-01-30 02:58

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

nohup python -u ./cmd.py > cmd.log &
查看更多
走好不送
3楼-- · 2020-01-30 03:05

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.

查看更多
相关推荐>>
4楼-- · 2020-01-30 03:10

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楼-- · 2020-01-30 03:16

Using '-u' with 'nohup' worked for me. 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 &
查看更多
欢心
6楼-- · 2020-01-30 03:24

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

查看更多
叼着烟拽天下
7楼-- · 2020-01-30 03:25
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

查看更多
登录 后发表回答