9

'init.d'在正在运行的脚本中使用 Python 是否有任何特殊注意事项init?(即启动 Ubuntu)

根据我在 Ubuntu 上通过谷歌搜索/测试了解到的情况,提供给'init.d'脚本的环境变量很少,因此使用"#!/usr/bin/env python"可能无法正常工作。

还要别的吗?

4

2 回答 2

4

这只是突出了 init.d 脚本中 python 的最大问题——增加了复杂性。

Python 没有规范,env 甚至不必指向 cpython。如果你升级和 python 中断,你将不得不咬你的舌头。并且 python 崩溃的可能性比 sh 大得多(对于 init.d 脚本来说是安全的选择)。原因是,简单的实用程序:

ecarroll@x60s:/etc/init.d$ ldd /usr/bin/python
    linux-gate.so.1 => (0xb7ff7000)
    libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0xb7fc9000)
    libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7fc5000)
    libutil.so.1 => /lib/tls/i686/cmov/libutil.so.1 (0xb7fc0000)
    libz.so.1 => /lib/libz.so.1 (0xb7faa000)
    libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7f84000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7e21000)
    /lib/ld-linux.so.2 (0xb7ff8000)
ecarroll@x60s:/etc/init.d$ ldd /bin/sh
    linux-gate.so.1 => (0xb803f000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7ec7000)
    /lib/ld-linux.so.2 (0xb8040000)

Python 正在链接到 libpthread、libdl、libutil、libz、libm 以及其他可能中断的东西。Python 只是做得更多。

-rwxr-xr-x 1 根 86K 2008-11-05 01:51 /bin/dash
-rwxr-xr-x 1 root root 2.2M 2009-04-18 21:53 /usr/bin/python2.6

You can read up more about what you're specifically talking about with env variables here: http://www.debian.org/doc/debian-policy/ch-opersys.html#s9.9 The main problem is that the defaults for env can be set in /etc/profile which would only run if the script is being run under a shell that supports reading it.

于 2009-09-10T15:32:56.917 回答
1

我假设这是运行某种用 python 编写的守护进程,如果不是,那么这可能不适用。

您将(可能)想做标准的 unix 双叉和重定向文件描述符的事情。这是我使用的一个(改编自一个 ActiveState 代码接收器,其 url 目前让我无法理解)。

def daemonize(stdin, stdout, stderr, pidfile):
    if os.path.isfile(pidfile):
        p = open(pidfile, "r")
        oldpid = p.read().strip()
        p.close()
        if os.path.isdir("/proc/%s"%oldpid):
            log.err("Server already running with pid %s"%oldpid)
            sys.exit(1)
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError, e:
        log.err("Fork #1 failed: (%d) %s"%(e.errno, e.strerror))
        sys.exit(1)
    os.chdir("/")
    os.umask(0)
    os.setsid()
    try:
        pid = os.fork()
        if pid > 0:
            if os.getuid() == 0:
                pidfile = open(pidfile, "w+")
                pidfile.write(str(pid))
                pidfile.close()
            sys.exit(0)
    except OSError, e:
        log.err("Fork #2 failed: (%d) %s"%(e.errno, e.strerror))
        sys.exit(1)
    try:
        os.setgid(grp.getgrnam("nogroup").gr_gid)
    except KeyError, e:
        log.err("Failed to get GID: %s"%e)
        sys.exit(1)
    except OSError, e:
        log.err("Failed to set GID: (%s) %s"%(e.errno, e.strerror))
        sys.exit(1)
    try:
        os.setuid(pwd.getpwnam("oracle").pw_uid)
    except KeyError, e:
        log.err("Failed to get UID: %s"%e)
        sys.exit(1)
    except OSError, e:
        log.err("Failed to set UID: (%s) %s"%(e.errno, e.strerror))
        sys.exit(1)
    for f in sys.stdout, sys.stderr:
        f.flush()
    si = open(stdin, "r")
    so = open(stdout, "a+")
    se = open(stderr, "a+", 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

只需在启动守护程序循环之前运行它,它可能会做正确的事情。

作为旁注,我在 ubuntu 上的脚本中使用 #!/usr/bin/env python 作为 shebang 行,它对我来说工作正常。

即使您没有运行守护程序来提供调试信息,您可能仍希望将 stdout/stderr 重定向到文件。

于 2009-09-10T14:27:05.733 回答