Nohup is not writing log to output file

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. Using -u will force the stdout, stderr streams 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 -u option. For using this run below commands before running python code.

    export PYTHONUNBUFFERED=1
    

    or

    export PYTHONUNBUFFERED=TRUE
    

P.S.- I will suggest using tools like cron-job to run things in the background and scheduled execution.

Tags:

Python

Nohup