Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @mark clarified it's a Linux system, the script could easily make itself fully independent, i.e., a <em>daemon</em>, by following this <a href="http://code.activestate.com/recipes/278731/" rel="nofollow noreferrer">recipe</a>. (You could also do it in the parent after an <code>os.fork</code> and only then <code>os.exec...</code> the child process).</p> <p>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 <code>fork</code>, <code>_exit</code> and <code>setsid</code> calls). The various <code>os.exec...</code> variants that do <em>not</em> end in <code>e</code> use the parent process's environment, so that part is easy too -- see <a href="http://docs.python.org/library/os.html#process-management" rel="nofollow noreferrer">Python online docs</a>.</p> <p>To address suggestions made in others' comments and answers: I believe <code>subprocess</code> and <code>multiprocessing</code> per se don't daemonize the child process, which seems to be what @mark needs; the script could do it for itself, but since <em>some</em> code has to be doing <code>fork</code>s and <code>setsid</code>, 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.</p> <p>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.</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload