Reputation: 3081
Boiling down to the smallest problem here is a simple python script that I want to run using nohup on linux. I run it using the following (on linux):
nohup python test.py &
The command seems to just not do anything, nothing is appended to nohup.out. If I run it without the & the output shows correctly on the terminal window. What am I missing?
import time
def test():
while(True):
print "Woke up!"
time.sleep(5)
if __name__ == "__main__":
test()
Upvotes: 15
Views: 13998
Reputation: 11
I had the same problem, however, my algorithm was a little more complex than yours demonstrated. As I had created a python virtual environment by installing all the necessary packages for its execution, it only runs if the virtual environment is activated source env/bin/activate
. After activating the environment, I was able to run normally, not resulting in any errors: nohup python3 -u scriptShow.py > logScriptNohup.log &
.
If you want to make sure it's running, do in the terminal: ps aux | grep scriptShow
. If the running process appears, congratulations, you did it
Upvotes: 1
Reputation: 5573
Just had a similar issue. My script worked well without nohup. With nohup, the script would crash with a SyntaxError.
The problem was the execution context of nohup which would use an alias of python
mapped to python2
instead of python3
.
Fixed it by specifying python3
instead of python
.
Upvotes: 3
Reputation: 2128
Pass python the -u flag for unbuffering stdout
nohup python -u test.py &
Python will buffer stdout otherwise. This doesn't require a code change.
From the man page:
-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout
and stderr in binary mode. Note that there is internal buffering in xreadlines(), readlines() and file-object
iterators ("for line in sys.stdin") which is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline()" inside a "while 1:" loop.
Upvotes: 31
Reputation: 74675
You need to flush stdout
after printing: sys.stdout.flush()
; otherwise it'll take awhile to fill the stdout buffer.
Upvotes: 4