How to properly terminate a process that is running in the background via subprocess?

37 Views Asked by At

I have method that runs a bash script in the background via subprocess.Popen():

def my_method():
        try:
            process = subprocess.Popen(["bash", "my_script.sh", "&" ],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            )
        except Exception as e:
            print(f"Error running the script: {e}")
        return process

def caller():
        process = my_method()

Now, I want the caller method to properly terminate the background script. I tried several solutions like process.kill() and process.terminate(). But, none of them works. I was wondering is the best solution for this?

0

There are 0 best solutions below