Python子进程在守护程序化时不执行

2024-09-29 17:20:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在父类被守护的方法中执行脚本。在

在自动伽马.sh需要安装ImageMagick(并使用convert)的脚本,可以在此处找到:http://www.fmwconcepts.com/imagemagick/autogamma/index.php

import os
import subprocess
import daemon

class MyClass():
    def __init__(self):
        self.myfunc()
    def myfunc(self):
        script = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'autogamma.sh')
        cmd = ('/bin/sh %s -c average /tmp/c.jpg /tmp/d.jpg' % script).split(' ')
        ret = subprocess.Popen(cmd).communicate()

with daemon.DaemonContext():
    process = MyClass()
    process.run()

脚本在启动类MyClass时正确执行。我认为env或类似的东西有问题,但无法得到它。在

Rsync,mediainfo,ffprobe也出现了问题。 使用python2.7.3和Python守护进程1.6,在macos、centos5.5、ubuntu12.04TLS上测试


Tags: pathimportself脚本cmdosdefsh
3条回答

这个脚本很短,如果不包括用于读取命令行参数、注释和其他颜色模式的代码,它将少于75行。我只需要把它转换成Python。在

如注释所示,最好的方法是为ImageMagick使用一个python包装器。在

您也可以直接调用convert,尽管这可能会很痛苦。下面是一个小片段,内容如下:

import subprocess

def image_magick_version():
    output = subprocess.check_output("/usr/local/bin/convert -list configure", shell=True)

    for line in output.split('\n'):
        if line.startswith('LIB_VERSION_NUMBER'):           
            _, version = line.split(' ', 1)
            return tuple(int(i) for i in version.split(','))

im_version = image_magick_version()    
if im_version < (6,7,6,6) or im_version > (6,7,7,7) :
    cspace = "RGB"
else:
    cspace = "sRGB"

if im_version < (6,7,6,7) or im_version > (6,7,7,7):
    setcspace = "-set colorspace RGB"
else:
    setcspace = ""

我终于发现了问题。这实际上是一个路径问题。在查看了lib之后,我发现了这个有用的参数:

    `working_directory`
        :Default: ``'/'``

        Full path of the working directory to which the process should
        change on daemon start.

        Since a filesystem cannot be unmounted if a process has its
        current working directory on that filesystem, this should either
        be left at default or set to a directory that is a sensible “home
        directory” for the daemon while it is running.

所以我把守护程序设置成这样:

^{pr2}$

现在我有了正确的路径,我的脚本得到了正确的执行。在

当我怀疑环境问题时,我会做两件事之一:

  1. 使用“env-whatever script”在前台运行脚本。这个 应清除环境,并将错误发送到终端的 斯特德尔。在
  2. 正常运行脚本,但将stdout和stderr重定向到 /tmp:what script>;/tmp/output 2>;1中的文件

这使得tty-less脚本的不透明性有所降低。在

相关问题 更多 >

    热门问题