To close all subprocesses in Python, use the subprocess.Popen() function. The subprocess.Popen() is a built-in Python function that executes a child program in a new process.
import os import subprocess import signal p1 = subprocess.Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid) os.killpg(os.getpgid(p1.pid), signal.SIGTERM)
In this example, we saw how to kill all the subprocesses using the killpg command. The killpg() method send the signal to all the process group to terminate the process. Among all these commands, killpg is the method used to kill all the subprocesses. We can also use for loop to kill all the processes by using the kill() method or terminate() method.
What is a subprocess in Python
The subprocess is a built-in Python module that can be used to create a subprocess using two methods in which, one is using the run() method and another using the Popen() method.
How to create subprocess in Python
To create a subprocess in Python, use the run() method. The run() command is described by args. It waits for the command to complete, then returns a CompletedProcess instance.
import subprocess subprocess.run(‘dir’,shell=True)
You can also use the Popen() method to create a subprocess in Python.
import subprocess subprocess.Popen([“echo”,”hello”])
Many arguments can be passed inside the Popen() method. The Popen() function is similar to the open() function we use to open the file using the os module. This can be used to open other programming language files also. The file is opened, and the output can be generated using this Popen() method.
How to close a single subprocess in Python
To close a single subprocess in Python, use the kill() method. The kill() is a built-in method used for terminating a single subprocess. The kill() command keeps running in the background.
import subprocess p1 = subprocess.Popen("exec "+ "clear", stdout=subprocess.PIPE, shell=True) p1.kill() print("The cmd is close")
The cmd is close
You can see that the kill() method terminates the single subprocess.
Kill single subprocess using terminate() method.
To kill a single subprocess in Python, use the subprocess.terminate() method. The subprocess.terminate() is a built-in Python method used to close the single process created by the user.
import subprocess p1 = subprocess.Popen(["echo", "hello"]) p1.terminate() print("No output from echo")
No output from echo
Now, let us see an example for closing the firefox browser from the terminal:
import os import signal process_name = input("Enter the process name: ") try: for f in os.Popen("ps ax | grep ” + name + “ | grep –v grep "): line = f.split() id = line os.kill(int(id), signal.SIGKILL) print(" process is killed ") except: print(" Error ")
In this example, if we give the process name as firefox, all the instances of the firefox will be killed. This example shows how to kill the process by using the process name.
That’s how you close all subprocesses in the Python tutorial.