To use a pipe with the subprocess module, you can pass shell=True but be aware of the Security Considerations. It is discouraged using shell=True. In most cases there are better solutions for the same problem.

However, this isn't really advisable for various reasons, not least of which is security. Instead, create the ps and grep processes separately, and pipe the output from one into the other, like so:

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

In your particular case, however, the simple solution is to call subprocess.check_output(('ps', '-A')) and then str.find on the output.

Answer from Taymon on Stack Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
5 days ago - Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...
Discussions

How to get output from subprocess.Popen along with visible execution?
I am trying to get the output by running below dummy code for my project - #test.py import time for x in range(5): print(i) time.sleep(1) This is test2.py import subprocess cmd = "python3 test.py" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=true, text=true) ... More on discuss.python.org
🌐 discuss.python.org
6
1
July 22, 2024
Calling read on a subprocess pipe makes select block even when data is available
Hello! I have been using Python to launch child processes, to read from their output and to write to their input. I have been almost successfully doing that for a while, until I encountered some problems with graceful shutdown of the parent process. I decided then to use select in order to ... More on discuss.python.org
🌐 discuss.python.org
19
0
November 11, 2024
subprocess.Popen and output
To avoid deadlock, I think you are supposed to say handbrake.communicate() if using pipes. But I'm not familiar with your technique of using pipes to suppress output, either. IME, pipes are for capturing output. There's a DEVNULL object for suppression. It's either stdout or stderr, at least on a UNIX. More on reddit.com
🌐 r/learnpython
18
22
December 7, 2022
How to get output from subprocess.Popen along with visible execution?
I am trying to get the output by running below dummy code for my project - #test.py import time for x in range(5): print(i) time.sleep(1) This is test2.py import subprocess cmd = "python3 test.py" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=true, text=true) ... More on google.com
🌐 google.com
6
1
July 22, 2024
🌐
Rednafi
rednafi.com › python › unix-style-pipeline-with-subprocess
Unix-style pipelining with Python's subprocess module | Redowan's Reflections
July 14, 2023 - The ps -ef command outputs a full list of running processes, then the pipe symbol sends that output as input to the head -5 command, which reads the first 5 lines from that input and prints just those, essentially slicing off the top 5 processes.
🌐
Lyceum-allotments
lyceum-allotments.github.io › 2017 › 03 › python-and-pipes-part-5-subprocesses-and-pipes
Python and Pipes Part 5: Subprocesses and Pipes
March 2, 2017 - By passing the constant subprocess.PIPE as either of them you specify that you want the resultant Popen object to have control of child proccess’s stdin and/or stdout, through the Popen’s stdin and stdout attributes.
🌐
GitHub
gist.github.com › waylan › 2353749
Writing to a python subprocess pipe · GitHub
This seemed like the most obvious solution but it fails miserably. It seems that the first call to communicate also closes the pipe and the second loop raises an exception. from subprocess import Popen, PIPE p = Popen('less', stdin=PIPE) for x in xrange(100): p.stdin.write('Line number %d.\n' % x)
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - The stdout of the ls command is connected to the stdin of the grep command using subprocess.PIPE, which creates a pipe between the two processes. The communicate() method is used to send the output of the ls command to the grep command and retrieve the filtered output.
🌐
GitHub
gist.github.com › nawatts › e2cdca610463200c12eac2a14efc0bfb
capture-and-print-subprocess-output.py · GitHub
Passing stdout=subprocess.PIPE, stderr=subprocess.STDOUT to subprocess.run captures the output but does not let the subprocess print. So you don't see any output until the subprocess has completed. Redirecting sys.stdout or sys.stderr doesn't work because it only replaces the Python script's stdout or stderr, it doesn't have an effect on the subprocess'.
🌐
Python
docs.python.org › 3 › library › asyncio-subprocess.html
Subprocesses — Python 3.14.3 documentation
February 23, 2026 - See also the Subprocess and Threads section. ... Wait for the child process to terminate. Set and return the returncode attribute. ... This method can deadlock when using stdout=PIPE or stderr=PIPE and the child process generates so much output that it blocks waiting for the OS pipe buffer to accept more data.
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - So, for the rest of the pipe examples, only UNIX-based examples will be used, as the basic mechanism is the same for both systems. They’re not nearly as common on Windows, anyway. If you want to let the shell take care of piping processes into one another, then you can just pass the whole string as a command into subprocess: ... >>> import subprocess >>> subprocess.run(["sh" , "-c", "ls /usr/bin | grep python...
🌐
Python.org
discuss.python.org › python help
Calling read on a subprocess pipe makes select block even when data is available - Python Help - Discussions on Python.org
November 11, 2024 - Hello! I have been using Python to launch child processes, to read from their output and to write to their input. I have been almost successfully doing that for a while, until I encountered some problems with graceful shutdown of the parent process. I decided then to use select in order to ...
🌐
Alexwlchan
alexwlchan.net › notes › 2025 › subprocess-line-by-line
How to stream lines from stdout with subprocess – alexwlchan
May 17, 2025 - import subprocess args = ["yt-dlp", "--get-id", "https://www.youtube.com/watch?list=PLCbA9r6ecYWU6SVyvb32a0YHIzpr9jxnW"] proc = subprocess.Popen(args, stdout=subprocess.PIPE, bufsize=1, text=True) for line in proc.stdout: print(line) proc.poll() print(proc.returncode)
🌐
Reddit
reddit.com › r/learnpython › subprocess.popen and output
r/learnpython on Reddit: subprocess.Popen and output
December 7, 2022 -

I'm trying to grok subprocess.Popen to run and monitor a command line executable in the background. (Specifically, the HandbrakeCLI video converter.)

In this I have been partially successful, using the following command:

handbrake = subprocess.Popen( cmd )

Where cmd is a list of parameters. When I do this, I can do other things while it's running, poll() it, and terminate it if desired, and unless I kill it, it runs to completion exactly the way I want. The problem is that I also want to suppress the output. No problem, right?

handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE )

This works, but there's still SOME output. Now, my first thought was that the messages I was seeing were techincally ERROR messages. So I tried two different methods:

handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )

In both cases, the subprocess launches, I can do other things, but... it doesn't do anything. I can see HandbrakeCLI in the task manager, but it's not using any resources (where it *should* be using nearly 100%), and no file has been created in the target directory.

This leaves me with two questions that may or may not be related:

  1. Why is redirecting stdout causing the program to do nothing?

  2. Where is that other output coming from, and how do I suppress it? (Helpfully, it doesn't contain any information I need to capture.)