I have tried to use subprocess.check_output() for getting the ps aux command using python but it looks like not working with the large grep string. Can anyone have any solution?
subprocess.check_output('ps aux | grep "bin/scrapy" | grep "option1" | grep "option2" | grep "option3" | grep "option4" | grep "option5"' , shell =True)
Advertisement
Answer
Yes, I have found the solution to achieve the output, We can use the following code snippet to find the PID as output.
Instead of using ps aux we can use psutil python library.
import psutil args_list = ["option1", "option2", "option3"] for process in psutil.process_iter(): try: process_args_list = process.cmdline() except psutil._exceptions.NoSuchProcess as err: logger.info(f"Found error in psutil, Error: {err}") continue if all(item in process_args_list for item in args_list): print(process.pid)