Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:
   #    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?

like image 411
Jo Ko Avatar asked May 19 '17 22:05

Jo Ko


People also ask

How to run a Linux process in the background with nohup?

To run a Linux process in the background with the nohup command, add the & symbol at the end of the command: For example, to run the example.sh bash script in the background, use the command:

What is the nohup command in Linux?

Using the nohup command is one way of blocking the SIGHUP signal and allowing processes to complete even after logging out from the terminal/shell. In this article, you will learn how to use the nohup command while running processes.

Why does nohup take 10 seconds to return to session?

is not running in daemon mode, keeping your ssh session connected. Ssh session will return in 10 seconds, despite you used nohup. Reason is that remote stdout and stderr still connected to your session. It keeps ssh session alive, nohup does not help. returns immediately. It does start remote process with nohup and exits ssh session immediately.

Why am I getting a message that nohup is not working?

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. Before running the script with nohup make sure that the script itself works fine (This would actually be the first thing to try). Show activity on this post.


2 Answers

I usually face this in three cases:

  1. When an exception in the while loop occurs.

  2. When somewhere in the code I have pdb.set_trace() which is a debugging breakpoint.

  3. 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

  1. Before running the script with nohup make sure that the script itself works fine (This would actually be the first thing to try).
like image 81
Ash Avatar answered Oct 07 '22 20:10

Ash


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.

like image 25
Fujiao Liu Avatar answered Oct 07 '22 18:10

Fujiao Liu