0

The following script does not return tty

Isn't sys.exit(0) enough from parent process?
Do I have to redirect stdin/stdout inside a child process to return tty? If so, how do i do that?

[Partial code]

pid = os.fork()
if pid > 0:
    # parent
    sys.exit(0)

#child
PID = os.getpid()
PID_DIR = '/var/pid/'
PID_FILE = '%s,%s.pid' % (machine_name, PID)

with PidFile(pidname=PID_FILE, piddir=PID_DIR):
    worker_handler = gevent.spawn(worker)
    master_handler = gevent.spawn(master)
    handlers = [worker_handler, master_handler]

    try:
        while True:
            time.sleep(1)
    except Exception, msg:
        print msg
        gevent.killall(handlers)

    handlers.joinall()
Daerdemandt
  • 2,281
  • 18
  • 19
ealeon
  • 12,074
  • 24
  • 92
  • 173

1 Answers1

1

Isn't sys.exit(0) enough from parent process?

No, because child process has inherited file descriptors, meaning it's still connected to those stdin and stdout.

Do I have to redirect stdin/stdout inside a child process to return tty? If so, how do i do that?

Yes, you should do something like that. Actually, you should do some more stuff - like using double fork - but it was already implemented by other people, so you can just use that instead.

Quick and dirty, you can do the following in the child:

os.close(0)   # close C's stdin stream
os.close(1)   # close C's stdout stream
os.close(2)   # close C's stderr stream
Daerdemandt
  • 2,281
  • 18
  • 19