Notes about Python

From IdeaNet
Jump to: navigation, search
  • list of locations searched when importing modules
$ python -c "import sys; print sys.path"
  • how to create a daemon
do a double fork, change id, add a wait to avoid a defunct
def fork_exec_disown(cmd, rep):
    try:

        if os.fork()==0:

            if os.fork():
                sys.exit(0)

            os.chdir(os.path.expanduser(rep))
            os.setsid()
            os.umask(0)

            cmd = cmd.split()
            os.execvp(cmd[0], cmd)

        os.wait()

    except OSError, e:
        print >>sys.stderr, \
              "%s : Cannot change dir to '%s' and run '%s'" % (e, rep, cmd)