Psutil can't detect chrome processes from inside python script

The following script is fine for windows but for Ubuntu, it only works on Python shell when I ran the code inside my script it did not detect chrome processes. Your help will be appreciated. Here is my code:

def kill_processes():
    print('Process killing started...')
    proc_list = []
    for proc in psutil.process_iter():
        if "chrome" in proc.name().lower() or "uc_driver" in proc.name().lower():
            proc_list.append(proc)
            proc.terminate()
            print(proc)
            time.sleep(1)  # Adjust timing as needed
            if proc.is_running():
                proc.kill()
    gone, alive = psutil.wait_procs(
        proc_list, timeout=3)  # Adjust timing as needed
    for proc in alive:
        proc.kill()

It sounds like your script might be running into permissions issues or differences in the environment between the Python shell and script execution. Here are a few things to check:

Permissions: Run the script with sudo to ensure it has sufficient permissions:

bash

sudo python your_script.py

Process Names: Verify that the process names you're checking ("chrome" and "uc_driver") match exactly what is reported by psutil on Ubuntu.

Error Handling: Add error handling to catch exceptions that might be occurring silently.

Try these steps and see if they resolve the issue.