linux - Python subprocess command with pipe -
i want use subprocess.check_output() ps -a | grep 'process_name'. tried various solutions far nothing worked. can guide me how it?
to use pipe subprocess module, have pass shell=true.
however, isn't advisable various reasons, not least of security. instead, create ps , grep processes separately, , pipe output 1 other, so:
ps = subprocess.popen(('ps', '-a'), stdout=subprocess.pipe) output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout) ps.wait() in particular case, however, simple solution call subprocess.check_output(('ps', '-a')) , str.find on output.
Comments
Post a Comment