Abdul Rehman
Abdul Rehman

Reputation: 5654

Run terminal commands from python using Subprocess

I'm trying to run a terminal command from my django but getting an error.

Here's the command I want to run:

kubectl apply -f <(istioctl kube-inject -f FILE_PATH)

Here's what I have tried: From views.py

nodesvc = subprocess.call('kubectl apply -f <(istioctl kube-inject -f ' + os.path.join(path) + '/nodeService.yaml)',
                                      shell=True)

I'm creating a file in temporary directory and pass it's path here, which is correct.

Here's the error :

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `kubectl apply -f <(istioctl kube-inject -f /var/folders/g2/8fzl8mjj5xxfqdfvrl7v3q_40000gn/T/tmpstfcq3es/nodeService.yaml)'

I think that something wrong with curly braces '(', How can I resole this issue, help me please! Thanks in advance!

Upvotes: 2

Views: 1893

Answers (1)

W.Mann
W.Mann

Reputation: 923

The problem is that whatever shell python is invoking does not have support for process substitution via <(..). What works for me:

subprocess.call(["/bin/bash", "-c", "wc -l <(sort something.txt)"])

This forces to invoke the shell as bash, which usually supports process substitution. For your command, this should work:

subprocess.call(["/bin/bash", "-c", 'kubectl apply -f <(istioctl kube-inject -f ' + os.path.join(path) + '/nodeService.yaml)'])

Edit: adapt for question

Upvotes: 2

Related Questions