I have a simple python script that just runs an infinite while loop and prints "running". When I start it up with nohup in the background, I see a nohup.out file in my current directory but nothing ever gets written to it. I am confused. I have tried the following
nohup test.py
writes to nohup.out but obviously does not run in the background
nohup test.py &
runs in background but does not write to nohup.out
nohup test.py 2>&1 &
also runs in the background but does not write to nohup.out
I bet I am missing something simple. Any ideas?
Here is my python script for reference:
import sys
from time import sleep
def main():
print "Starting...."
while True:
print "running..."
sleep(5.00)
if __name__ == "__main__":
main()
By default, nohup runs a process in the foreground while redirecting the output to nohup. out file. The file is located in the current working directory unless the user does not have write permissions in that directory. In that case, the nohup.
You can delete it if you don't want what's in it, but if the program that created the file is still running, then the disk space won't actually be reclaimed until the program exits.
The 2>&1 redirects standard error to standard output, which you then capture in a file. If the command attempts to read from the terminal while in the background, it will be stopped.
Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup. out file is used as the default file for stdout and stderr.
Maybe your stdout is not flushed immediately, because it is not attached to a terminal. Try using
import sys
sys.stdout.flush()
to flush your output.
In python3 you can use flush=True
as an argument to the print function to do that.
print("running...", flush=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With