'psutil is not behaving same on mac and linux

I have one code which helps to get all process running via process name.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

import psutil
import time
#import datetime
def checkIfProcessRunning(processName):
    '''
    Check if there is any running process that contains the given name processName.
    '''
    #Iterate over the all the running process
    for proc in psutil.process_iter():
        try:
            # Check if process name contains the given name string.
            if processName.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False;
def findProcessIdByName(processName):
    '''
    Get a list of all the PIDs of a all the running process whose name contains
    the given string processName
    '''
    listOfProcessObjects = []
    #Iterate over the all the running process
    for proc in psutil.process_iter():
       try:
           pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
           # Check if process name contains the given name string.
           if processName.lower() in pinfo['name'].lower() :
               listOfProcessObjects.append(pinfo)
       except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
           pass
    return listOfProcessObjects;
def main():
    print("*** Check if a process is running or not ***")
    # Check if any chrome process was running or not.
    if checkIfProcessRunning('chrome'):
        print('Yes a chrome process was running')
    else:
        print('No chrome process was running')
    print("*** Find PIDs of a running process by Name ***")
    # Find PIDs od all the running instances of process that contains 'chrome' in it's name
    listOfProcessIds = findProcessIdByName('chrome')
    if len(listOfProcessIds) > 0:
       print('Process Exists | PID and other details are')
       for elem in listOfProcessIds:
           #print(elem)
           processID = elem['pid']
           processName = elem['name']
           processCreationTime =  time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(elem['create_time']))
           #print(type(processCreationTime))
           #print((processID ,processName,processCreationTime ))
           test_time = time.time() - elem['create_time']
           timeout = 1
           #print("test time",test_time)
           #print("current time",time.time())
           #print("create time",elem['create_time'])
           if test_time > timeout:
               print("I will kill this process", processID, processName)
    else :
       print('No Running Process found with given text')
    print('** Find running process by name using List comprehension **')
    # Find PIDs od all the running instances of process that contains 'chrome' in it's name
    #procObjList = [procObj for procObj in psutil.process_iter() if 'chrome' in procObj.name().lower() ]
    #for elem in procObjList:
     #  print (elem)
if __name__ == '__main__':
   main()

It is working fine when i am trying on mac. Getting this output. (1116, 'Google Chrome Helper (Renderer)', '2022-02-02 09:56:18') I will kill this process 1116 Google Chrome Helper (Renderer) (1121, 'Google Chrome Helper (Renderer)', '2022-02-02 09:56:19') I will kill this process 1121 Google Chrome Helper (Renderer) (1149, 'Google Chrome Helper (Renderer)', '2022-02-02 09:56:20') I will kill this process 1149 Google Chrome Helper (Renderer) (1153, 'Google Chrome Helper (Renderer)', '2022-02-02 09:56:20')

But same code is not working on linux. i am trying to check for archivelog_to_oss process getting this output

No archivelog_to_oss process was running
*** Find PIDs of a running process by Name ***
No Running Process found with given text
** Find running process by name using List comprehension **```
But process is running
root      53825  53823  3 12:00 pts/4    00:00:04 python /opt/faops/spe/ocifabackup/bin/rman_wrapper.py -b archivelog_to_oss
root     116633  53825  0 12:01 pts/4    00:00:00 su oracle -c /bin/bash /opt/faops/spe/ocifabackup/bin/rman_oss.sh --dbname=phxyqko -b archivelog_to_oss --retention-days=0
oracle   116635 116633  0 12:01 ?        00:00:00 /bin/ba


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source