Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 231145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:54:31+00:00 2026-05-11T19:54:31+00:00

im spawning a script that runs for a long time from a web app

  • 0

im spawning a script that runs for a long time from a web app like this:

os.spawnle(os.P_NOWAIT, "../bin/producenotify.py", "producenotify.py", "xx",os.environ)

the script is spawned successfully and it runs, but till it gets over i am not able to free the port that is used by the web app, or in other words i am not able to restart the web app. how do i spawn off a process and make it completely independent of the web app?

this is on linux os.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:54:32+00:00Added an answer on May 11, 2026 at 7:54 pm

    As @mark clarified it’s a Linux system, the script could easily make itself fully independent, i.e., a daemon, by following this recipe. (You could also do it in the parent after an os.fork and only then os.exec... the child process).

    Edit: to clarify some details wrt @mark’s comment on my answer: super-user privileges are not needed to “daemonize” a process as per the cookbook recipes, nor is there any need to change the current working directory (though the code in the recipe does do that and more, that’s not the crucial part — rather it’s the proper logic sequence of fork, _exit and setsid calls). The various os.exec... variants that do not end in e use the parent process’s environment, so that part is easy too — see Python online docs.

    To address suggestions made in others’ comments and answers: I believe subprocess and multiprocessing per se don’t daemonize the child process, which seems to be what @mark needs; the script could do it for itself, but since some code has to be doing forks and setsid, it seems neater to me to keep all of the spawning on that low-level plane rather than mix some high-level and some low-level code in the course of the operation.

    Here’s a vastly reduced and simplified version of the recipe at the above URL, tailored to be called in the parent to spawn a daemon child — this way, the code can be used to execute non-Python executables just as well. As given, the code should meet the needs @mark explained, of course it can be tailored in many ways — I strongly recommend reading the original recipe and its comments and discussions, as well as the books it recommends, for more information.

    import os
    import sys
    
    def spawnDaemon(path_to_executable, *args)
        """Spawn a completely detached subprocess (i.e., a daemon).
    
        E.g. for mark:
        spawnDaemon("../bin/producenotify.py", "producenotify.py", "xx")
        """
        # fork the first time (to make a non-session-leader child process)
        try:
            pid = os.fork()
        except OSError, e:
            raise RuntimeError("1st fork failed: %s [%d]" % (e.strerror, e.errno))
        if pid != 0:
            # parent (calling) process is all done
            return
    
        # detach from controlling terminal (to make child a session-leader)
        os.setsid()
        try:
            pid = os.fork()
        except OSError, e:
            raise RuntimeError("2nd fork failed: %s [%d]" % (e.strerror, e.errno))
            raise Exception, "%s [%d]" % (e.strerror, e.errno)
        if pid != 0:
            # child process is all done
            os._exit(0)
    
        # grandchild process now non-session-leader, detached from parent
        # grandchild process must now close all open files
        try:
            maxfd = os.sysconf("SC_OPEN_MAX")
        except (AttributeError, ValueError):
            maxfd = 1024
    
        for fd in range(maxfd):
            try:
               os.close(fd)
            except OSError: # ERROR, fd wasn't open to begin with (ignored)
               pass
    
        # redirect stdin, stdout and stderr to /dev/null
        os.open(os.devnull, os.O_RDWR)  # standard input (0)
        os.dup2(0, 1)
        os.dup2(0, 2)
    
        # and finally let's execute the executable for the daemon!
        try:
          os.execv(path_to_executable, args)
        except Exception, e:
          # oops, we're cut off from the world, let's just give up
          os._exit(255)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Web services in NAV 2009 can call either CodeUnit or… May 11, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer You can use the Windows Mobile Device Security Manager to… May 11, 2026 at 11:28 pm
  • Editorial Team
    Editorial Team added an answer The short answer is "no"; but this is a very… May 11, 2026 at 11:28 pm

Related Questions

I'd like to parse rss feeds and download podcasts on my ReadyNas which is
I'm spawning a child process that runs in a visible console window (it's a
I have a GUI application within which i'm spawning a console application using Process
Currently, I'm spawning a message box with a OS-library function (Windows.h), which magically keeps

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.