【问题标题】:Special considerations for using Python in init.d script?在 init.d 脚本中使用 Python 的特殊注意事项?
【发布时间】:2010-11-27 04:38:58
【问题描述】:

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

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

还有什么?

【问题讨论】:

    标签: python linux ubuntu init.d


    【解决方案1】:

    这只是突出了 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

    您可以在此处详细了解您具体谈论的环境变量: http://www.debian.org/doc/debian-policy/ch-opersys.html#s9.9 主要问题是 env 的默认值可以在 /etc/profile 中设置,只有在脚本在支持读取它的 shell 下运行时才会运行。

    【讨论】:

      【解决方案2】:

      我假设这是在运行某种用 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 重定向到文件。

      【讨论】:

      • 初始化脚本不是守护进程。
      • Peter - 我指的是 /etc/init.d/ 目录中的一个脚本(在启动时执行);上述脚本是用 Python 编写的(因为我受不了 bash)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2015-06-25
      • 2016-10-16
      • 2010-12-31
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多