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?
You can run Python with the
-u
flag to avoid output buffering:It looks like you need to flush stdout periodically (e.g.
sys.stdout.flush()
). In my testing Python doesn't automatically do this even withprint
until the program exits.Python 3.3 and above has a flush argument to print and this is the only method that worked for me.
Using '-u' with 'nohup' worked for me. Everything will be saved in "nohup.out " file. Like this-
You can also save it into your directory. This way-
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:
PS: my scripts were written in ksh running on RHEL
or
https://docs.python.org/2/using/cmdline.html#cmdoption-u