2017-12-14 21 views
1

다중 처리 프로세스가 시작될 플라스크 응용 프로그램을 배포하고 있습니다. 이 프로세스 내에서 쉘 명령 물마루 subprocess.call()을 호출합니다. 여기 배포 된 웹 응용 프로그램에서 셸 명령을 호출 할 때 예기치 않은 동작이 발생했습니다.

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() 

가의 rellevant 일부입니다 nginx를하고 gunicorn 함께 배포 로컬 호스트에 실행될 때 스크립트는 잘 실행, 플라스크 앱이 하위 프로세스가 시작 예상대로 다르게 동작 할 때까지, 나는 다음과 같은 오류 로그를받을 기능.

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) 

명령 줄에서 "명령"을 호출하려고하면 문제없이 작동합니다.

답변

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

이 완료 문자열

+0

안녕, 대답에 대한 감사 구하십시오. 예, 오류가 사라지 긴하지만 명령은 여전히 ​​실행되지 않습니다. 나는이 명령에서 p = subprocess.run ("ls", shell = True, stdout = PIPE), logging.debug (p.stdout)를 사용하여 stdout을 잡았다. 로그 파일에 아무 것도 의미하지 않는 DEBUG : root : b " '라는 메시지가 나타납니다. 명령이 실행되지 않은 것으로 보입니다 –

+0

지금 얻고있는 오류는 무엇입니까 –

+0

아니요 더 이상 오류가 없습니다. os.system (명령)을 시도했지만 아무 일도 일어나지 않지만 터미널에서 명령을 실행하면 완벽하게 작동합니다. –

1

로 적절한 솔루션을 명령을 실행하는 데 도움이됩니다은 구문 분석 된 인수 목록으로 명령을 전달하는 것입니다. 이 작업을 수행하려면 셸에서 인용 및 인수 분리를 처리하는 방법을 이해해야합니다. 쉘에서 빠른 속임수로

,

printf "'%s'\n" your command line here 

당신에게 쉘이 인수를 확장하는 방법의 좋은 아이디어를 줄 것이다. 예를 들어

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)) 
+1

https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess 또한 왜'shell = True'를 피하기를 원하십니까 – tripleee

+0

고마워요! 정말 도움이됩니다! –