Nohup is not writing log to output file
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dream Voyager Looping
--
Chapters
00:00 Question
00:39 Accepted answer (Score 112)
01:00 Answer 2 (Score 503)
01:15 Answer 3 (Score 72)
02:12 Answer 4 (Score 20)
02:29 Thank you
--
Full question
https://stackoverflow.com/questions/1291...
Answer 3 links:
https://docs.python.org/2/using/cmdline....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #nohup
#avk47
ANSWER 1
Score 532
You can run Python with the -u flag to avoid output buffering:
nohup python -u ./cmd.py > cmd.log &
ACCEPTED ANSWER
Score 119
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.
ANSWER 3
Score 81
To run a Python script in the background and ensure that output is saved, you can use nohup along with the -u option to force the stdout and stderr streams to be unbuffered. This allows you to see real-time output in the nohup.out file.
What is nohup?
nohup stands for "no hang up." It is a POSIX command used to run another command or script in the background, even after logging out from the session. When you use nohup, the command continues to run after the session ends, making it ideal for long-running processes.
How to Use nohup with Python
To run your Python script with unbuffered output using nohup, use the following command:
nohup python -u your_code.py &
This command will save the output in the default nohup.out file in the current directory. If you want to save the output to a specific directory, you can redirect it like this:
nohup python -u your_code.py > /your_directory/nohup.out &
Using PYTHONUNBUFFERED
Alternatively, you can achieve the same unbuffered output behavior by setting the PYTHONUNBUFFERED environment variable. This variable works similarly to the -u option:
Before running your Python script, set the environment variable:
export PYTHONUNBUFFERED=1
Or:
export PYTHONUNBUFFERED=TRUE
Then, run your script with nohup:
nohup python your_code.py &
OS Dependency
The nohup command is primarily used in Unix-like operating systems such as Linux and macOS. On Windows, nohup is not available natively. However, you can achieve similar functionality using the start command or running scripts as background tasks using PowerShell or Task Scheduler. If you need to run long-running processes on Windows, consider using a virtual machine or WSL (Windows Subsystem for Linux) where nohup is available.
Additional Tip
For scheduled execution or managing background tasks, consider using tools like cron jobs on Unix-like systems. These tools provide more flexibility for running scripts at specific intervals or in the background.
ANSWER 4
Score 22
export PYTHONUNBUFFERED=1
nohup ./cmd.py > cmd.log &
or
nohup python -u ./cmd.py > cmd.log &