Sure, you can find a slightly updated version of the code in the attachment, licensed under the two clause BSD license and without the syntax error.<br><div class="gmail_quote"><div><br>Thank you very much for being willing to share this code under the BSD license.  I will try this out and see if it does the job for us.<br>
<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It would be even better if you could try to integrate the functionality into Twisted itself, if the core developers agree that it would be useful. I opened a ticket for this:<br>
<br>
<a href="http://twistedmatrix.com/trac/ticket/4073" target="_blank">http://twistedmatrix.com/trac/ticket/4073</a><br>
<br></blockquote><div><br>Yes, it would be nice if this was in twisted itself.  I will probably include it in IPython for now and see how it works for us - use that as a medium term test of the approach.  I will provide feeback and any changes to the twisted ticket as appropriate.<br>
<br>Thanks again,<br><br>Brian<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Regards,<br>
Ziga<br>
<br># Copyright (c) 2009 Ziga Seilnacht. All rights reserved.<br>
#<br>
# Redistribution and use in source and binary forms, with or without<br>
# modification, are permitted provided that the following conditions are met:<br>
#<br>
# 1. Redistributions of source code must retain the above copyright notice,<br>
#    this list of conditions and the following disclaimer.<br>
# 2. Redistributions in binary form must reproduce the above copyright notice,<br>
#    this list of conditions and the following disclaimer in the documentation<br>
#    and/or other materials provided with the distribution.<br>
#<br>
# THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT &#39;&#39;AS IS&#39;&#39; AND ANY EXPRESS OR<br>
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF<br>
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO<br>
# EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,<br>
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,<br>
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,<br>
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF<br>
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE<br>
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF<br>
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
<br>
<br>
import os<br>
import sys<br>
import msvcrt<br>
<br>
try:<br>
    import win32con<br>
    import win32process<br>
    import win32security<br>
    import win32service<br>
except ImportError:<br>
    win32process = None<br>
<br>
from twisted.python import win32<br>
<br>
<br>
<br>
def getPythonArgs():<br>
    &quot;&quot;&quot;<br>
    Return the list of command line args that were used to start<br>
    the current Python interpreter and were not stored in C{sys.argv}.<br>
<br>
    These are the options that control the Python interpreter itself,<br>
    like the Python executable, optimization level, warning filters,<br>
    division behaviour and literal string handling.<br>
    &quot;&quot;&quot;<br>
    args = [sys.executable]<br>
    for warnoption in sys.warnoptions:<br>
        args.append(&quot;-W&quot;)<br>
        args.append(warnoption)<br>
    if type(1 / 2) is not int:<br>
        args.append(&quot;-Qnew&quot;)<br>
    if type(&quot;&quot;) is not str:<br>
        args.append(&quot;-U&quot;)<br>
    if not __debug__:<br>
        if getPythonArgs.__doc__ is None:<br>
            args.append(&quot;-OO&quot;)<br>
        else:<br>
            args.append(&quot;-O&quot;)<br>
    return args<br>
<br>
<br>
<br>
def daemonize():<br>
    args = [os.path.abspath(__file__)] + sys.argv<br>
    executable = sys.executable<br>
    cmdline = win32.quoteArguments(getPythonArgs() + args)<br>
    inherit = False<br>
    priority = win32process.GetPriorityClass(win32process.GetCurrentProcess())<br>
    flags = (win32process.CREATE_BREAKAWAY_FROM_JOB | # session leader<br>
             win32process.CREATE_NEW_PROCESS_GROUP |  # group leader<br>
             win32process.DETACHED_PROCESS |          # no controlling terminal<br>
             priority)<br>
    info = win32process.STARTUPINFO()<br>
    win32process.CreateProcess(executable, cmdline, None, None,<br>
                               inherit, flags, None, None, info)<br>
    # Do what exec* functions do, let the OS do the cleanup.<br>
    os._exit(0)<br>
<br>
<br>
<br>
def daemonize2():<br>
    args = [sys.argv[1], &quot;--nodaemon&quot;] + sys.argv[2:]<br>
    executable = sys.executable<br>
    cmdline = win32.quoteArguments(getPythonArgs() + args)<br>
    inherit = True<br>
    priority = win32process.GetPriorityClass(win32process.GetCurrentProcess())<br>
    flags = (win32process.CREATE_NO_WINDOW | # create an invisible console<br>
             win32process.CREATE_NEW_PROCESS_GROUP | # group leader<br>
             priority)<br>
    attributes = win32security.SECURITY_ATTRIBUTES()<br>
    attributes.bInheritHandle = True<br>
    station = win32service.CreateWindowStation(None, 0,<br>
                                               win32con.GENERIC_READ |<br>
                                               win32con.GENERIC_WRITE,<br>
                                               attributes)<br>
    station.SetProcessWindowStation()<br>
    sname = win32service.GetUserObjectInformation(station,<br>
                                                  win32service.UOI_NAME)<br>
    dname = str(os.getpid())<br>
    desktop = win32service.CreateDesktop(dname, 0,<br>
                                         win32con.GENERIC_READ |<br>
                                         win32con.GENERIC_WRITE,<br>
                                         attributes)<br>
    desktop.SetThreadDesktop()<br>
    null = os.open(&quot;NUL&quot;, os.O_RDWR)<br>
    handle = msvcrt.get_osfhandle(null)<br>
    info = win32process.STARTUPINFO()<br>
    info.lpDesktop = &quot;%s\\%s&quot; % (sname, dname)<br>
    info.dwFlags = win32process.STARTF_USESTDHANDLES<br>
    info.hStdInput = info.hStdOutput = info.hStdError = handle<br>
    win32process.CreateProcess(executable, cmdline, None, None,<br>
                               inherit, flags, None, None, info)<br>
    # Same as above, exit as fast as posible.<br>
    os._exit(0)<br>
<br>
<br>
<br>
if __name__ == &quot;__main__&quot;:<br>
    daemonize2()<br>
<br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br>