Comando de subproceso Python con tubería


Quiero usar subprocess.check_output() con ps -A | grep 'process_name'. Probé varias soluciones, pero hasta ahora nada funcionó. ¿Puede alguien guiarme cómo hacerlo?

Author: zuberuber, 2012-11-11

7 answers

Para usar una tubería con el módulo subprocess, debe pasar shell=True.

Sin embargo, esto no es realmente recomendable por varias razones, entre ellas la seguridad. En su lugar, cree los procesos ps y grep por separado, y canalice la salida de uno a otro, de la siguiente manera:

ps = subprocess.Popen(('ps', '-A'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout)
ps.wait()

En su caso particular, sin embargo, la solución simple es llamar subprocess.check_output(('ps', '-A')) y luego str.find en la salida.

 299
Author: Taymon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-11 14:58:02

O puede usar siempre el método communicate en objetos de subproceso.

cmd = "ps -A|grep 'process_name'"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
print output

El método Communicate devuelve en una tupla la salida estándar y el error estándar.

 38
Author: jkalivas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-11 16:35:06

Consulte la documentación sobre cómo configurar una canalización mediante subproceso: http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline

No he probado el siguiente ejemplo de código, pero debería ser aproximadamente lo que desea:

query = "process_name"
ps_process = Popen(["ps", "-A"], stdout=PIPE)
grep_process = Popen(["grep", query], stdin=ps_process.stdout, stdout=PIPE)
ps_process.stdout.close()  # Allow ps_process to receive a SIGPIPE if grep_process exits.
output = grep_process.communicate()[0]
 18
Author: AlcubierreDrive,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-16 01:12:21

También, intente usar el comando 'pgrep' en lugar de 'ps -A | grep 'process_name'

 3
Author: Shooe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-13 10:34:47

La solución JKALAVIS es buena, sin embargo, agregaría una mejora al usar shlex en lugar de SHELL=TRUE. debajo de im greping out tiempos de consulta

#!/bin/python
import subprocess
import shlex

cmd = "dig @8.8.4.4 +notcp www.google.com|grep 'Query'"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
print output

Salud,

 3
Author: Daniel Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-28 01:09:36

Puede probar la funcionalidad de tubería en sh.py :

import sh
print sh.grep(sh.ps("-ax"), "process_name")
 2
Author: amoffat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-12 05:54:58

Puedes probar

check_output(["sh", "-c", "ps", "-A", "|", "grep", "process_name"])

O

check_output(["bash", "-c", "ps", "-A", "|", "grep", "process_name"])
 -3
Author: GangNanTed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-15 21:51:30