In this article, you will learn to run Python Script Constantly in the Background. I’ll show you a variety of methods for keeping Python scripts running in the background on various platforms.
The Python scripting language (files with the extension.py) is run by the python.exe program by default. This application launches a terminal window, which remains open even if the program utilizes a graphical user interface (GUI).
If you do not want this to happen, use the extension.pyw, which will cause the script to be executed by pythonw.exe by default. This prevents the terminal window from appearing when the computer is first booted.
Let us see in the below example codes how you can run Python Script Constantly In Background.
1. Using sched To Run Python Script Constantly In Background
In Python, you have a library named sched that acts as a Scheduler and allows you to run a script or program in Python continuously. Events can be scheduled using the scheduler class’s generic interface. When it comes to dealing with the “outside world,” two functions are needed: timefunc, which is callable without parameters and returns a number.
Let us see in the below example code the usage of sched to run Python script constantly.
import sched import time schedulerTest = sched.scheduler(time.time, time.sleep) def randomFunc(test = 'default'): print("Testing Scheduler to Run Script Constantly.") schedulerTest.enter(30, 1, randomFunc, ('checkThis',)) schedulerTest.enter(30, 1, randomFunc, ('TestThis',)) schedulerTest.run()
Testing Scheduler to Run Script Constantly. Testing Scheduler to Run Script Constantly.
If you are going to use the above code, and add the function that you want to run constantly then you can use the scheduler library.
2. Using the time.sleep() Function To Run Script repeatedly
So, if you do not want to use the above code and just want to run your script repeatedly then you can use the time.sleep() function. This function allows your script to keep running after sleeping for a certain amount of time.
Let see in the below example code the usage of time.sleep() function to run Python Script Repeatedly.
import time while True: def randomFunctionYouWantToKeepRunning(): print("Testing To Run Script in Background Every 10 secs.") randomFunctionYouWantToKeepRunning() time.sleep(10)
Testing To Run Script in Background Every 10 secs. Testing To Run Script in Background Every 10 secs.
As you can see using the above code, I was able to run the script constantly after every 10 seconds. Based on your usage and after how much time you want the program to re-run you can change the time and then re-run the script.
How To Run Python Script In Background In Linux
You can use the below code on your terminal command line to run the Python Script in the background in Linux. You can also use the code described above to run the Python script constantly.
First, add a Shebang line in your Python Script using the below code.
If you have numerous versions of Python installed, this path is required to ensure that the first Python interpreter listed in your $$PATH environment variable is taken into consideration. Your Python interpreter’s location can alternatively be hardcoded, although this is less flexible and less able to be used on different machines.
Now run the below command to make the Shebang executable.
chmod +x yourPythonScriptName.py
If you use no hangup, the application will continue to execute in the background even if your terminal is closed.
nohup /path/to/yourPythonScriptName.py & or nohup python /path/to/test.py &
Here & is used as this will tell the operating system to put this process to run in the background.
How To Run Python Script In Background In Windows
If you want to run any Python script in Background in Windows operating System then all you are required to do is to rename the extension of your existing Python script that you want to run in background to ‘.pyw’.
Once you have re-named it then you can run the Python Script and it will start executing in the background. To Terminate the program that started running in the background you will require to use the below command.
TASKKILL /F /IM pythonw.exe
I hope you got your answer and learned about how to run Python Scripts Constantly In the Background Of Windows, Linux or MacOS. I have discussed two methods and you can use any two methods that work perfectly for you.
Let me know in the comment section if you know any better method than the one discussed above, I will be happy to add it here. Also, let me know if you have any issues related to the above code.
- How To Convert Python String To Array
- Code Example: How To Check If Set Is Empty In Python
- Code Examples – Write A List To CSV Using Python
- Code Example: Loop Back To The Beginning Of A Program
- How To Do Nothing In An If Statement Python
- Code Example: Remove The First Item From List Python
- 4 Ways To Loop Through List With Index Using Python
- Code Examples: Multiply In Python