1

I'm using subprocess() on AWS lambda And using this layer: https://github.com/lambci/git-lambda-layer

Here is code:

import json
import os

import subprocess

def lambda_handler(event, context):

    os.chdir('/tmp')

    subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
    subprocess.Popen(["touch word.txt"], shell=True)

    word = str(subprocess.check_output(["ls"], shell=True))
    return {
        'statusCode': 200,
        'body': json.dumps(word)
    }

And it returns:

Response:
{
  "statusCode": 200,
  "body": "\"b'word.txt\\\\n'\""
}

So there is something wrong on subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)

I checked there is git by subprocess.check_output(["git --version"], shell=True) and it works well.

How to solve it?

1
  • subprocess.Popen is non-blocking. You may want to use subprocess.run. And btw, If shell is True, it is recommended to pass args as a string rather than as a sequence. Commented Jun 2, 2020 at 17:09

1 Answer 1

3

There are a few problems.

First, you need to wait for the git process to exit. To do this with subprocess.Popen, call .wait() on the returned Popen object. However, I'd recommend using subprocess.check_call() instead to automatically wait for the process to exit and to raise an error if the process returns a non-zero exit status.

Second, there's no need to specify shell=True since you're not using any shell expansions or built-ins. In fact, when passing an argument list when using shell=True, the first item is the command string, and the remaining items are arguments to the shell itself, not the command.

Lastly, you're missing a slash in your GitHub URL.

Try this instead:

subprocess.check_call(["git", "clone", "https://github.com/hongmingu/requirements"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.