Run Background Process in Python

Background processes are computer processes that run behind the scenes. The core advantage of running processes in the background is that you can do other tasks in the foreground. In this article, we will discuss how to run background processes using Python.  We will also discuss how to check and stop running processes/applications.

Checking Running Processes and Services

To check the running processes in Windows, you can use the Task Manager App or run the command “tasklist” on the command line.

Some processes or services start when the computer is started, while the user explicitly starts others. A process can also create another process. For example, a Python process is begun when running a Python script on an editor or an IDE like Sublime Text or Visual Studio.  The rest of the processes running under Sublime Text in the Task Manager below are just processes supporting the two main processes.

The problem is that once Sublime Text is closed, all these processes are closed – including the program being executed in Python. This is where understanding how to run the background process comes in.

Running background processes in Python

In Python, any of the two packages, namely, subprocess and os, can be used to run background processes. The general syntax is:

subprocess:

os:

To demonstrate how these packages work in running a background process, let’s run a Python script. Even if the code editor is closed, the python.exe process will keep running in the background until the execution is complete. This is because the Python process is not attached to the process holding the editor alive.

Step 1: Create a Python file with the code we are interested in running in the background

Create a python script “process.py” with the following code in it:

The code should run for around 100 seconds and write a text file (results.txt) into the disk with numbers 0 through 4 written in it. If you execute this file on the editor directly and accidentally or otherwise close the editor, the execution process is also terminated. This is because closing the editor also closes the Python process, which is responsible for executing the code.

Step 2: Create another Python script that will wake up the Python process in the background

To ensure that the code on “process.py” is executed in the background until completion, let us create another file Python script called “run_process.py” and use the two packages, subprocess, and os, to run the code in “process.py” in the background.

The file “run_process.py” contains the following lines of codes based on the choice of the package to use:

subprocess package:

and, for the os package, we have

Step 3: Execute the run_process.py file on the code editor

Note that a Python background process running “process.py” is initiated when you execute “run_process.py” on the code editor. This process remains awake until the execution of the “process.py” script is completed, even if you close the editor.

This process locks the results.txt file, so if you try to delete it during the program operation, you will be greeted with the message that the file is in use, so you can either wait or kill the process.

Killing a running process

We need the Image Name or the Process ID (PID) to kill a running process. This information can be found on the Task Manager or by running the tasklist on the command line. You might need to right-click on the title section and select PID and Process name on Task Manager (see below).

Any of the following options can be used to terminate a running process (refer to the above screenshot of Task Manager for the following points).

Using Task Manager:

select the process, right-click, and click “end task” at the bottom right of the manager window.

On the command prompt:

  • when using PID run “taskkill /PID <pid> /PID <another_pid> /F“, for example, “taskkill /PID 11024 /F” to kill CTF Loader.
  • When using Image Name, run “taskkill /IM <ImageName> /IM <another_ImageName> /F”  for example “taskkill /IM ctfmon.exe /F” to kill CTF Loader.

In Python

You can kill running tasks using subprocess or os as follows (note that you need to import these packages):

  • subprocess.Popen([“taskkill”, “/PID”, “11024”, “/F”])
  • os.system(“taskkill /IM ctfmon.exe /F”)

Note: The /F option issued to the taskkill command is optional. It is used to terminate a process forcefully. This option might be needed when closing a task used by another process, for example.

The PID changes every time you open the process/app. This means that the PID used to kill a task generated by a given app may not work when an app is closed and opened again. In such a case, Image Name is a better option to use.

Conclusion

In this article, we discussed how to wake up a process and keep running in the background using Python. We have demonstrated how this is done by running a python.exe process in the background (you can do this with any process). Lastly, we have shown how to terminate a running process(es) using Task Manager, command prompt, and Python packages.