0

I'm looking to check if a process is running by name using subprocess to Popen "powershell ps | findstr processname" and do something if it's running/not running. I don't want to accomplish this using psutil.

so far this is the code I've came up with.

import subprocess
import sys

p = subprocess.Popen(['powershell.exe', 'ps | findstr chrome'], 
stdout=subprocess.PIPE)
output = p.stdout.read()
s = output.split()

chrome = "chrome"
for _ in s:
    if chrome in s:
        print("chrome running")
    else:
        print("chrome not running")

This doesn't seem to work. Does anyone know how to accomplish this task?

1 Answer 1

1
import subprocess
import sys

p = subprocess.Popen(['powershell.exe', 'ps | findstr chrome'],
stdout=subprocess.PIPE)
output = p.stdout.read()
s = output.split()

chrome = b"chrome"

if chrome in s:
    print("chrome running")
else:
    print("chrome not running")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.