Reputation: 7575
I have a python script, practice_one.py
, that I’d like it to run forever in Ubuntu Linux, and has the following:
while True:
# Code
And I attempted nohup python practice_one.py &
but got the message nohup: ignoring input and appending output to ‘nohup.out’
.
Then when I hit the enter key, the outputs another message: [1]+ Exit nohup python practice_one.py
How come it just exits automatically? What could I be doing wrong?
EDIT
Attempted:
nohup python practice_one.py </dev/null &>/dev/null &
And get [1] 61122
then when I press enter I got [1]+ Exit nohup python practice_one.py </dev/null &>/dev/null &
It used to work but now exits.. What could be the issue now?
Upvotes: 14
Views: 6029
Reputation: 1
I just met this issue. I am pretty sure there is something wrong with your code. Otherwise, there should not be the exit message.
Upvotes: 0
Reputation: 1482
I agree with Ash, you're facing some trouble within your script.
I've tested your scenario and I assure you nohup isn't the problem:
Python code
$ cat practice_one.py
while True:
print 'ok'
Nohup execution
m.ortiz.montealegre@CPX-XYR3G1DTHBU ~/python_excercises/nohup-python
$ nohup python practice_one.py &
[1] 10552
m.ortiz.montealegre@CPX-XYR3G1DTHBU ~/python_excercises/nohup-python
$ nohup: ignoring input and appending output to 'nohup.out'
m.ortiz.montealegre@CPX-XYR3G1DTHBU ~/python_excercises/nohup-python
$ ps
PID PPID PGID WINPID TTY UID STIME COMMAND
10552 15248 10552 9992 cons1 6758389 16:52:27 /usr/bin/python2.7
So, try simplifying your script operations to find out which one is the problem by commenting the code and test until you find the culprit.
Upvotes: 3
Reputation: 69198
Set execution permission on the script
chmod +x practice_one.py
Try closing all the file descriptors
nohup ./practice_one.py <&- 1>&- 2>&- &
Upvotes: 2
Reputation: 2253
I suggest that you'd better use double fork magic
to run your long-running program as a daemon instead of nohup
. It is easy to debug and your program doesn't need external tools like nohup
.
daemon.py
import sys
import os
# double fork magic
def daemonize():
os.umask(0)
try:
pid = os.fork()
except OSError:
sys.exit(0)
if pid > 0:
sys.exit(0)
os.setsid()
try:
pid = os.fork()
except OSError:
sys.exit(0)
if pid > 0:
os._exit(0)
os.chdir('/')
import resource # Resource usage information.
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd == resource.RLIM_INFINITY):
maxfd = 1024
# Iterate through and close all file descriptors.
for fd in range(0, maxfd):
try:
os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass
fd = os.open(os.devnull, os.O_RDWR)
os.dup2(fd, sys.stdin.fileno())
os.dup2(fd, sys.stdout.fileno())
os.dup2(fd, sys.stderr.fileno())
test.py
from daemon import daemonize
import time
def test():
while True:
time.sleep(10)
if __name__ == '__main__':
daemonize() # you could comment this line before you make sure your program run as you expect
test()
Now, using python test.py
to make it run as a daemon and you could use ps aux | grep test.py
to check it.
Upvotes: 5
Reputation: 3550
I usually face this in three cases:
When an exception in the while loop occurs.
When somewhere in the code I have pdb.set_trace() which is a debugging breakpoint.
When somewhere in the code, input from user is requested as in raw_input("Please enter something: ")
Check your code to exclude these three causes.
Don't worry about the first message, it is just informing you that nohup is in work.
EDIT
nohup
make sure that the script itself works fine (This would actually be the first thing to try).Upvotes: 9
Reputation: 870
This is a normal behaviour of nohup. To avoid this try:
nohup python my_script.py </dev/null &>/dev/null &
Upvotes: -1
Reputation: 21
In the code, there should be a break. What i mean by that is; You should have a exit state even though you're not going to reach it. In the exist state, you can specify to make the while loop become true.
Source: https://wiki.python.org/moin/WhileLoop
I'd recommend to use a for loop instead.
Upvotes: 1
Reputation: 25779
The first is not an error - it just means that nohup will capture STDOUT and store it in nohup.out. If you want to mute everything use:
nohup python practice_one.py &>/dev/null
Upvotes: 1