~ / archivo / stackoverflow / 44080033
Python + Ubuntu Linux + nohup error: [1]+ Exit
-
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: # CodeAnd I attempted
nohup python practice_one.py &but got the messagenohup: 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.pyHow 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] 61122then 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?
- Klaus D. 2017-05-20 02:06 Look into
nohup.outfor any error message. - moooeeeep 2017-05-22 06:57 You do have any code in the body of that loop, right?
- Jamil Said 2017-05-24 19:31 I would start working on this issue by adding a python shebang on the script, such as
#!/usr/bin/env python. Next, I would run thisbashcommand to make sure that the script is executable for all users:chmod +x practice_one.py. Next, I would add a simple write to an external file right after the shebang but BEFORE anything else, such aswith open("my_file.txt", "w") as file: file.write("OK"), and then I would try to run the file again and see if the external file had the "ok" printed on it. If you try this all, reply to me in this tread with the result so I can try to help further - Chibueze Opata 2017-05-28 17:18 Could you post your script somewhere? Maybe it's possible to learn something new.
- Dinesh 2017-06-29 04:54 If "It used to work but now exits.." then the problem cannot be in the driver. Everyone has been there done that :-)
- Klaus D. 2017-05-20 02:06 Look into
-
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
- Before running the script with
nohupmake sure that the script itself works fine (This would actually be the first thing to try).
- Jesse Adelman 2017-06-28 00:22 This is the "answer", because the question contains too little detail to say otherwise. My bet is #4.... :)
-
I suggest that you'd better use
double fork magicto run your long-running program as a daemon instead ofnohup. It is easy to debug and your program doesn't need external tools likenohup.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.pyto make it run as a daemon and you could useps aux | grep test.pyto check it. -
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.7So, try simplifying your script operations to find out which one is the problem by commenting the code and test until you find the culprit.
-
Set execution permission on the script
chmod +x practice_one.pyTry closing all the file descriptors
nohup ./practice_one.py <&- 1>&- 2>&- & -
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- Jo Ko 2017-05-19 23:31 Everything got muted but how do I check to see if it is in fact being ran?
- zwer 2017-05-19 23:35 Ummm,
ps aux | grep python? - Jo Ko 2017-05-19 23:45 Why is it that strings like 4EAST is recognized as 4%EF%BF%EF%BF%BD etc.
- zwer 2017-05-19 23:57 That depends on text and shell encoding, terminal representation etc. plus is a bit out of scope of your original question.
- Jo Ko 2017-05-19 23:58 when I run the command to see if nohup is being ran, what should it say on the list? Don't believe I see any nohup
- zwer 2017-05-20 00:02 The above command will show you a list of processes with python in their name. One of them should end with something like:
/usr/bin/python practice_one.py(nohup won't be mentioned). That is if your script didn't exit in the mean time (and since we're muting the output above we cannot know why).
-
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.
-
Bronco respuesta 2019-02-05 21:03
I just met this issue. I am pretty sure there is something wrong with your code. Otherwise, there should not be the exit message.
-
This is a normal behaviour of nohup. To avoid this try:
nohup python my_script.py </dev/null &>/dev/null &- Riccardo Petraglia 2017-05-23 21:34 Do not solve the problem... (the fact that the script exit)