Let My Terminal Go

Mike Meyer mwm at mired.org
Tue Oct 11 03:12:20 EDT 2005


"mystilleef at gmail.com" <mystilleef at gmail.com> writes:

> Hello,
>
> A user of my application points me to a behavior in gVim,
> the text editor, that I would like to implement in my
> application.
>
> When gVim is launched from a shell terminal, it completely
> frees the terminal. You can continue to use the terminal for
> whatever purpose you wish, including closing and exiting it,
> without any effect on the running gVim instance.
>
> How do I implement this in my application written in python?
> I would like to believe it does not involve me forking my
> application in a new process. Maybe there is signal I can
> send to the operating system to achieve this, right?

Several things need to happen. 

First, you need to take yourself out of the session you are in. To do
that, you use the setsid system call. This is available in python as
os.setsid.

Last, you need to detach your process from the terminal. You do that
by closing all the file descriptors you have that reference it. stdin,
stdout and stderr should do the trick. The standard trick is to set
set them to /dev/null. This has to happen last, so that if there are
problems in the second step, writing to stderr about it does some
good.

Second, you need to tell the shell that launched you that it can
continue. The standard way to do this is to fork your process, and
have the parent exit. That causes the parent shell to think your
process is dead, and so forget about it completely. There are other
ways to do this, but they aren't as reliable.

The easy way to do all these things - from C, anyway - is with
daemon(3). That isn't wrapped as part of the Python library. The
easiest way to solve your problem may be write a wrapper for that
call. If daemon exists on enough systems, submitting your wrapper as a
patch to the os modulee would be appropriate.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list