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 ?
1 Answer 1
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.
1 Comment
/bin/sh is not necessarily bash. It may be other Bourne shell.
shell=True?kill $(ps | grep process | awk '{ print 1ドル}' | head -n1)"usepkill process?