Skip to content

Instantly share code, notes, and snippets.

@isaaguilar
Last active July 5, 2017 07:37
Show Gist options
  • Save isaaguilar/401589929d5704b087b29cd46605fd42 to your computer and use it in GitHub Desktop.
Save isaaguilar/401589929d5704b087b29cd46605fd42 to your computer and use it in GitHub Desktop.
a version of `ps ax` for python
import re
from subprocess import Popen, PIPE
# Equivilent to `ps ax|grep search_string`
p2 = Popen(["grep", search_string], stdin=PIPE, stdout=PIPE) # search_string
p1 = Popen(["ps", "ax"], stdout=p2.stdin)
# Break up results into invidual lines
results = p2.communicate()[0].split("\n")
# Now iterate thru the list, filter out grep, and print pids
pids = []
for line in results:
if "grep" in line:
continue
pids.append(re.sub("\ .*$", "", line.lstrip(" ")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment