bg and fg

Stefan Antoni sasoft at gmx.de
Sun Nov 4 17:21:18 EST 2001


On Sun, Nov 04, 2001 at 08:44:07AM -0600, Chris Gonnerman wrote:

> > - i am using linux
> > - i got a script that takes the time.ctime string and
> >   compares it with a set time. if the time is the same
> >   as the set time, it will do something.
> > - the script is a non-gui application (see bottom of this mail)
> > 
> > question:
> > 1. how can i move my process in the background after i
> >    started it (like: myscript & -> but automatically).
> 
> if os.fork():
>     sys.exit(0)
> 
> ... is the basic code (full daemon mode takes a bit more work).
Ok, thx. I wrote a little daemon-skelleton with using an example
on python.faqts.com :

#!/usr/bin/env python

import sys, os, time

if os.fork() == 0:
	os.setsid # what does THAT mean? it was in the example.
	          # i looked into "pydoc os" but i didn't understand
		  # find an explantation
	sys.stdout = open("/dev/null", 'w')
	sys.stdin = open("/dev/null", 'r')

	while 1:
	  pass
	  # daemon code: 

It works :) 
... but raises another question:
How can i send commands to a daemon to (let's say) get 
it's status or terminate it without killing it?
Do i need a local socket connection for doing something like this?
Or is there a more elegant way?

> > 2. how do i move it back in the foreground when the
> >    action for the set time is performed?
> 
> You can't.  The shell does it by keeping a "grip" on the process,
> and so it can return it to the foreground when told to.  AFAIK you
> can't tell the shell to do this programmatically.
I think this has to do with security(?)

-- 
thx in advance,
Stefan Antoni




More information about the Python-list mailing list