在已部署的Web应用程序上调用shell命令时出现意外行为

时间:2017-12-14 17:40:55

标签: python shell flask subprocess multiprocessing

我正在部署一个烧瓶应用程序,其中将启动multiprocessing.Process。在这个过程中,我通过subprocess.call()调用一个shell命令。当在本地主机上执行时,脚本运行正常,当使用nginx和gunicorn部署时,随着子进程启动,烧瓶应用程序按预期运行,然后我收到以下错误日志:

DEBUG:root:start to run command
DEBUG:root:(<class 'FileNotFoundError'>, FileNotFoundError(2, "No such file or directory: 'java -jar ábsolute/path/to/jar/file')

                                Process(
                                target=decode_upload,
                                args=(
                                        path_to_blf,
                                        path_to_dbc_folder,
                                        path_to_splitted,
                                        path_to_decoder,
                                        system_layout,
                                        dc_doc,
                                        dc_id,
                                        file_type,
                                )
                            ).start()

这是函数的相关部分。

def decode_file(
    path_to_blf,
    path_to_dbc_folder,
    path_to_splitted,
    path_to_decoder,
    system_layout=DEFAULT_SYSTEM_LAYOUT):
    command = "{} {} --blf={}".format(
                                        SOFTWARE_COMMAND,
                                        path_to_decoder,
                                        path_to_blf
    )
    for dbc_file_name in DBC_FILE_NAME_LIST:
        command += " --dbc={}".format(
                                    os.path.join(
                                            path_to_dbc_folder,
                                             dbc_file_name
                                    )
        )
    command += " --out={}".format(path_to_splitted)


    logging.debug("start to run command")
    subprocess.call(command)
    logging.debug(f)
    logging.debug("run command end")

def decode_upload(
                        path_to_blf,
                        path_to_dbc_folder,
                        path_to_splitted,
                        path_to_decoder,
                        system_layout,
                        dc_doc,
                        dc_id,
                        file_type):
    logging.basicConfig(filename='flask.log',level=logging.DEBUG)
    logging.debug('This message should go to the log file')

    try:
        decode_file(
            path_to_blf,
            path_to_dbc_folder,
            path_to_splitted,
            path_to_decoder,
            system_layout)
    except:
        logging.debug(sys.exc_info())

当到达此行时,该过程失败。

subprocess.call(command)

如果我尝试调用&#34;命令&#34;它从命令行运行没有问题。

2 个答案:

答案 0 :(得分:1)

from subprocess import Popen
command='your complete command as you paste on cmd'
p1=Popen(command,Shell=True)

这将帮助您将命令作为完整字符串

运行

答案 1 :(得分:1)

正确的解决方案是将命令作为已解析参数列表传递。您需要了解shell如何处理引用和参数拆分以正确执行此操作。

快速作弊,

printf "'%s'\n" your command line here
你的shell中的

应该让你很好地了解shell如何扩展参数。例如

bash$ printf "'%s'\n" java -jar "/path/complex path with spaces.jar" \* \>\<
'java'
'-jar'
'/path/complex path with spaces.jar'
'*'
'><'

向您显示您需要

subprocess.call(['java', '-jar', '/path/complex path with spaces.jar', '*', '><'])

为此调整代码,我们获取

command = [SOFTWARE_COMMAND, path_to_decoder, '--blf={}'.format(path_to_blf)]
for dbc_file_name in DBC_FILE_NAME_LIST:
    command.append("--dbc={}".format(
        os.path.join(path_to_dbc_folder, dbc_file_name)))
command.append("--out={}".format(path_to_splitted))