Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35422

Forking into the background (Linux)

From Olive <not0read0765@yopmail.com>
Newsgroups comp.lang.python
Subject Forking into the background (Linux)
Date 2012-12-24 01:50 +0100
Organization Aioe.org NNTP Server
Message-ID <kb88sk$1lq$1@speranza.aioe.org> (permalink)

Show all headers | View raw


My goal is to write a script that 1) write something to stdout; then 
fork into the background, closing the stdout (and stderr, stdin) pipe.

I have found this answer (forking -> setsid -> forking) 
http://stackoverflow.com/a/3356154

However the standard output of the child is still connected to the 
terminal. I would like that if we execute a subprocess.checkprocess on 
this program,  only "I would like to see this" is captured and that the 
program terminates when the parent exits.

#! /usr/bin/python2
import os,sys,time

print "I would like to see this"
pid = os.fork()
if (pid == 0): # The first child.
     # os.chdir("/")
    os.setsid()
    # os.umask(0)
    pid2 = os.fork()
    if (pid2 == 0):  # Second child
      print "I would like not see this"
      time.sleep(5)
    else:
      sys.exit()    #First child exists
else:           # Parent Code
   sys.exit()   # Parent exists

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Forking into the background (Linux) Olive <not0read0765@yopmail.com> - 2012-12-24 01:50 +0100
  Re: Forking into the background (Linux) Hans Mulder <hansmu@xs4all.nl> - 2012-12-24 02:56 +0100
    Making a Unix daemon process (was: Forking into the background (Linux)) Ben Finney <ben+python@benfinney.id.au> - 2012-12-24 18:17 +1100

csiph-web