Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35422 > unrolled thread
| Started by | Olive <not0read0765@yopmail.com> |
|---|---|
| First post | 2012-12-24 01:50 +0100 |
| Last post | 2012-12-24 18:17 +1100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Olive <not0read0765@yopmail.com> |
|---|---|
| Date | 2012-12-24 01:50 +0100 |
| Subject | Forking into the background (Linux) |
| Message-ID | <kb88sk$1lq$1@speranza.aioe.org> |
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
[toc] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-12-24 02:56 +0100 |
| Message-ID | <50d7b654$0$6941$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #35422 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2012-12-24 18:17 +1100 |
| Subject | Making a Unix daemon process (was: Forking into the background (Linux)) |
| Message-ID | <mailman.1243.1356333490.29569.python-list@python.org> |
| In reply to | #35429 |
Hans Mulder <hansmu@xs4all.nl> writes: > 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 You're following a path that leads to the desire for a “daemon” <URL:http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python/688448#688448>. > 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. It's even better when you look at it on PyPI <URL:http://pypi.python.org/pypi/python-daemon/> (note that PyPy is a Python implementation, PyPI is an index of Python packages). The discussion forum for ‘python-daemon’ development is at <URL:http://lists.alioth.debian.org/mailman/listinfo/python-daemon-devel>. -- \ “Faith may be defined briefly as an illogical belief in the | `\ occurrence of the improbable.” —Henry L. Mencken | _o__) | Ben Finney
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web