Forking into the background (Linux)

Discussion in 'Python' started by Olive, Dec 24, 2012.

  1. Olive

    Olive Guest

    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
     
    Olive, Dec 24, 2012
    #1
    1. Advertising

  2. Olive

    Hans Mulder Guest

    On 24/12/12 01:50:24, Olive wrote:
    > 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


    You could do this before forking:

    sys.stdin.close()
    sys.stdin = open('/dev/null', 'r')
    sys.stdout.close()
    sys.stdout = open('/dev/null', 'w')
    sys.stderr.close()
    sys.stderr = open('/dev/null', 'w')


    You may want to look at the python-daemon module on Pypy, which appears
    to do what you need, including some features you haven't asked for, yet.


    Hope this helps,

    -- HansM
     
    Hans Mulder, Dec 24, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mason66
    Replies:
    0
    Views:
    425
    mason66
    Jul 27, 2006
  2. JWL
    Replies:
    4
    Views:
    591
    Michael Winter
    Sep 26, 2006
  3. jc
    Replies:
    3
    Views:
    1,733
  4. jc
    Replies:
    1
    Views:
    1,371
    Neredbojias
    Mar 19, 2008
  5. bob Smith
    Replies:
    0
    Views:
    123
    bob Smith
    Oct 23, 2003
Loading...

Share This Page