hotips
hotips

Reputation: 2631

Execute a shell function from python subprocess

I'm trying to kill a specific process with a command which works well in the shell but not from python subprocess

import subprocess
subprocess.call(["kill", "$(ps | grep process | awk '{ print $1}' | head -n1)"], shell=False)

A work around would be to put this command into a shell script and run the shell script. Is it possible from python subprocess directly ?

Upvotes: 0

Views: 431

Answers (1)

Cristian
Cristian

Reputation: 171

import subprocess

subprocess.Popen("kill $(ps | grep ncat | awk '{print $1}' | head -n1)", shell=True)

In your example, you don't create a subprocess from bash, but at the same time you use $(...) which is a bash instruction. To be more precise, you create a kill process and pass it argument $(...) which is not precomputed.

The above example creates a bash process, and then tells it to interpret kill $(...). Bash converts $(...) to a value, and then it runs kill VALUE.

Upvotes: 2

Related Questions