I am using the subprocess module to find out if a process is running. But the result is different when the process to find does not exist.
For example, in the shell, if the process python test.py does not exist, the output of ps -ef|grep python|grep test|awk '{print $2}' is empty. But in python:
cmd="ps -ef|grep python|grep test|awk '{print $2}'"
vp=subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
r=vp.communicate()[0]
The output r is not None. It is the pid of the shell executing cmd.
So how to get the desired result?
psutilmodule for tasks like these.os.getpid(). If it matches you can ignore it because it is your current process.