I have a script that checks something on my PC every 5 minutes and I don’t want Python to show on my task tray. I use Windows as my operating system.
Is there any way to make Python run in the background and force it to not show in my task tray?
Xetnus
3532 gold badges4 silver badges13 bronze badges
asked Apr 23, 2009 at 21:07
If you run a console script using pythonw.exe
, it will neither display a window nor appear in the task bar. For example, I use the following command to launch ntlmaps on startup:
C:\BenBlank\Python2.6\pythonw.exe scripts/ntlmaps
Be aware, however, that there is no way to interact with the script, nor to terminate it save via the Task Manager.
answered Apr 23, 2009 at 21:15
Ben BlankBen Blank
55k28 gold badges127 silver badges157 bronze badges
3
Just another option you have:
You can create a shortcut to your Python script, then right-click the shortcut --> Properties --> Shortcut tab
There is a drop-down box under the Run option which lets you run the command minimized.
answered Apr 23, 2009 at 21:17
Jason CoonJason Coon
17.6k10 gold badges42 silver badges50 bronze badges
1
You could run it as a service. See here
answered May 1, 2009 at 8:07
dugresdugres
12.7k8 gold badges46 silver badges51 bronze badges
0
cron it on linux; schedule it on windows [control panel > scheduled tasks > Add scheduled task]
S.Lott
385k81 gold badges509 silver badges779 bronze badges
answered Apr 23, 2009 at 21:10
MahdeToMahdeTo
11k2 gold badges27 silver badges28 bronze badges
1
To Run python file from any where :
Step 1:
Create Shortcut of Python File.
Step 2:
Place Shortcut in this location C:\ProgramData\Microsoft\Windows\Start Menu\Programs
Step 3:
Now Right Click --> Go to Properties --> Shortcut --> Press any key on keyboard it will take one shortcut key
Step 4:
Now , Type the Shortcut key which you entered in previous step.
Step 5:
Check out Output! :)
answered Jun 29, 2020 at 5:05
Be ChampzzBe Champzz
4314 silver badges6 bronze badges
Look for Schedule Tasks in the control panel.
answered Apr 23, 2009 at 21:11
David BergerDavid Berger
12.4k6 gold badges38 silver badges51 bronze badges
3
I have a script that checks something on my PC every 5 minutes and I don’t want Python to show on my task tray. I use Windows as my operating system.
Is there any way to make Python run in the background and force it to not show in my task tray?
Xetnus
3532 gold badges4 silver badges13 bronze badges
asked Apr 23, 2009 at 21:07
If you run a console script using pythonw.exe
, it will neither display a window nor appear in the task bar. For example, I use the following command to launch ntlmaps on startup:
C:\BenBlank\Python2.6\pythonw.exe scripts/ntlmaps
Be aware, however, that there is no way to interact with the script, nor to terminate it save via the Task Manager.
answered Apr 23, 2009 at 21:15
Ben BlankBen Blank
55k28 gold badges127 silver badges157 bronze badges
3
Just another option you have:
You can create a shortcut to your Python script, then right-click the shortcut --> Properties --> Shortcut tab
There is a drop-down box under the Run option which lets you run the command minimized.
answered Apr 23, 2009 at 21:17
Jason CoonJason Coon
17.6k10 gold badges42 silver badges50 bronze badges
1
You could run it as a service. See here
answered May 1, 2009 at 8:07
dugresdugres
12.7k8 gold badges46 silver badges51 bronze badges
0
cron it on linux; schedule it on windows [control panel > scheduled tasks > Add scheduled task]
S.Lott
385k81 gold badges509 silver badges779 bronze badges
answered Apr 23, 2009 at 21:10
MahdeToMahdeTo
11k2 gold badges27 silver badges28 bronze badges
1
To Run python file from any where :
Step 1:
Create Shortcut of Python File.
Step 2:
Place Shortcut in this location C:\ProgramData\Microsoft\Windows\Start Menu\Programs
Step 3:
Now Right Click --> Go to Properties --> Shortcut --> Press any key on keyboard it will take one shortcut key
Step 4:
Now , Type the Shortcut key which you entered in previous step.
Step 5:
Check out Output! :)
answered Jun 29, 2020 at 5:05
Be ChampzzBe Champzz
4314 silver badges6 bronze badges
Look for Schedule Tasks in the control panel.
answered Apr 23, 2009 at 21:11
David BergerDavid Berger
12.4k6 gold badges38 silver badges51 bronze badges
3
In Windows, it’s possible to run a Python script as a background process that continues to run even after the user logs out or closes the terminal window. This is useful in scenarios where you want a script to run continuously, even if the user is not actively using the terminal. The following are some of the methods that can be used to run a Python script in the background on Windows:
Method 1: Using the Task Scheduler
This tutorial will guide you on how to run a Python script in the background on Windows using Task Scheduler. The Task Scheduler is a built-in Windows tool that allows you to schedule and automate tasks. We will create a new task that runs our Python script in the background at a specified interval.
Step 1: Create a Python Script
Create a new Python script that you want to run in the background. For example, we will create a script named «background_script.py» that prints «Hello, World!» every 10 seconds.
import time
while True:
print("Hello, World!")
time.sleep(10)
Save the script in a directory of your choice.
Step 2: Create a New Task
- Open the Task Scheduler by searching for «Task Scheduler» in the Start menu.
- Click on «Create Task» in the right-hand panel.
- Give your task a name and description.
- Under the «Security options» section, select «Run whether the user is logged on or not».
- Under the «Triggers» tab, click on «New» to create a new trigger.
- Select «Daily» and choose the time and frequency you want the script to run.
- Under the «Actions» tab, click on «New» to create a new action.
- Select «Start a program» as the action type.
- In the «Program/script» field, enter the path to your Python executable. For example, «C:\Python38\python.exe».
- In the «Add arguments» field, enter the path to your Python script. For example, «C:\scripts\background_script.py».
- Click on «OK» to save the action.
- Click on «OK» to save the task.
Step 3: Test the Task
- Right-click on your new task and select «Run».
- Wait for the scheduled time and check if the script runs in the background as expected.
Method 2: Running the Script as a Windows Service
In order to constantly run a Python script in the background on Windows, we can create a Windows service. A Windows service is a program that runs in the background, similar to a Unix daemon. A service can be started automatically when the computer starts up, and can be configured to run under a specific user account.
Here are the steps to create a Windows service to run a Python script:
-
Install the
pywin32
package: -
Create a new Python script that will run as the Windows service. In this example, we will create a script called
myservice.py
:import win32serviceutil import win32service import win32event import servicemanager import socket import time class MyService(win32serviceutil.ServiceFramework): _svc_name_ = 'MyService' _svc_display_name_ = 'My Service' def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.stop_event = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.stop_event) def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_RUNNING) hostname = socket.gethostname() while True: # Replace this with your actual script code with open('output.txt', 'a') as f: f.write('Hello from {} at {}\n'.format(self._svc_name_, time.time())) time.sleep(10) if __name__ == '__main__': win32serviceutil.HandleCommandLine(MyService)
-
In the
MyService
class, replace thewhile True
loop with your actual script code. Theoutput.txt
file is just an example of how you can write output to a file. -
Open a command prompt as an administrator and run the following command to install the service:
python myservice.py install
-
Start the service using the Services app in Windows, or by running the following command:
-
You can view the output of your script in the
output.txt
file.
That’s it! Your Python script is now running as a Windows service. You can stop the service using the Services app or by running the following command:
Method 3: Running the Script in a Separate Console Window
Here are the steps to run a Python script in a separate console window on Windows:
- Open Notepad or any text editor.
- Type in the following code:
import subprocess
subprocess.Popen(["cmd", "/k", "python your_script.py"])
- Replace
your_script.py
with the name of your Python script. - Save the file with a
.py
extension. - Open the Command Prompt by pressing
Win + R
and typingcmd
. - Navigate to the directory where your Python script is located using the
cd
command. - Type in the name of the file you just created and press Enter.
- Your Python script should now be running in a separate console window.
Here’s a breakdown of what the code does:
import subprocess
imports the subprocess module, which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.subprocess.Popen()
creates a new process and returns a Popen object that you can use to interact with the process. Thecmd
argument specifies that you want to open a new Command Prompt window. The/k
argument tells Command Prompt to keep the window open after the command has finished executing. The last argument is the command you want to run, which in this case ispython your_script.py
.
That’s it! You can now run your Python script in a separate console window on Windows.
Method 4: Using nohup Command
To constantly run a Python script in the background on Windows using the nohup command, you can use the following steps:
-
Install the Git for Windows tool on your machine, which includes the nohup command.
-
Open the command prompt and navigate to the directory where your Python script is located.
-
Use the following command to run your Python script in the background:
nohup python your_script.py &
This command will start your script in the background and redirect all output to a file called nohup.out.
- To check if your script is still running, you can use the following command:
This command will show a list of all running Python processes.
Here’s an example Python script that you can use to test this method:
import time
while True:
print("Hello, world!")
time.sleep(1)
This script will print «Hello, world!» every second.
Using the nohup command is a simple and effective way to run Python scripts in the background on Windows.
Are you wondering how to run a Python script in the background while you do other things? You can do so easily on both Linux and Windows by running the Python program as a service. That way, the program/service will start once the device powers on and stop as it shuts down, or you manually stop it.
Table of Contents
- 1. Why Learn How To Run a Python Script in the Background?
- 2. How To Run a Python Script in Linux
- 3. How To Run a Python Script in Windows
- 4. An Easier Alternative To Running Python Scripts for Scraping
Python remains the most popular programming language globally because of its easy syntax, but you’ll need a tutorial to run Python script as a service on Linux or Windows if you don’t have coding experience.
In this article, you’ll learn how to run Python code in the background on Linux and Windows. Once we’ve covered how to run a Python script for scraping data, we’ll show you how to do it the easy way!
Why Learn How To Run a Python Script in the Background?
One of the most frequent uses of Python is web scraping. Web scraping involves programs crawling websites (HTML code) to extract data. Businesses may want to collect data for research or competitor analysis, and they need to do it continuously. Many websites are dynamic and constantly update their data.
You can learn how to run a Python script as a service, but manually scraping data from such websites would require running the code repeatedly, which can be a hassle.
A better solution is to write a script for scraping data and run it in the background. You can specify the websites the program should visit, the data it should collect, and where it should store it.
You can continue to run other programs on your device and do other tasks while the scraping script does its job silently in the backdrop.
If you want to learn how to run a Python script in Linux distributions, you’ve come to the right place.
On Linux, you can run a Python script in the background using a terminal or the interface of any Linux distribution. The former is more straightforward, so let’s talk about how to run a Python file in a terminal.
Here’s how to run a Python script for scraping as a Linux daemon (service).
Step 1: Write a script for collecting HTML content of web pages
The first step is to write the script for parsing through the URLs of webpages and collect the HTML content. This script can run in a loop, refreshing the data every time. That way, it collects the latest data from the websites.
urls = [
‘url1’,
‘url2’,
]
index = 0
while True:
url = urls[index % len(urls)]
index += 1
print(‘Scraping url’, url)
response = requests.get(url)
Enter appropriate URLs in place of URL1, URL2, and so on.
Step 2: Parse data with the BeautifulSoup library
Once you have collected the HTML data from the URLs, it’s time to parse the data for valuable information. The BeautifulSoup Python library allows for data extraction from HTML and XML files. This step is much easier as you need only to use this library and save the information in JSON format:
soup = BeautifulSoup(response.content, ‘html.parser’)
Step 3: Handle shutdown requests
This is how to run a Python script without any disruptions: Add a class to ensure that the operating system lets an iteration complete its cycle before it is shut down.
For example:
class SignalHandler:
shutdown_requested = False
def __init__(self):
signal.signal(signal.SIGINT, self.request_shutdown)
signal.signal(signal.SIGTERM, self.request_shutdown)
def request_shutdown(self, *args):
print(‘Service stopping, shut down request received’)
self.shutdown_requested = True
def can_run(self):
return not self.shutdown_requested
Running Linux Daemon with systemd
Now that you know how to run a Python script in the background using the terminal, let’s go over an easier alternative. It’s also possible to run a Python script in Linux distributions using the user interface. However, that approach is not feasible as it doesn’t ensure the script runs after the restart. A better way is to use systemd, which is a service manager.
Create a file with the service extension in the system directory, cd /etc/systemd/system.
Using any editor, write the code for running the Python script by defining the Python execute file. Define parameters for running the service, such as After, Restart, and ExecStart.
Then, run the daemon-reload command to refresh and tell the system about the new service. Start the service.
How To Run a Python Script in Windows
It’s not as simple or easy to run a Python script as a Windows service compared to Linux. If you want to run Python script from the command line, you’ll need to make changes to the code to define how the script executes based on input from the command line. You’ll also need to set properties.
It can be quite complicated. A better way to run Python code as a service in Windows is to use Non-Sucking Service Manager. NSSM is an application that lets Windows users create a service using any executable code or batch script. Windows operating system treats it as a service, which can be managed and run with the help of services.msc in Command Prompt or Services option in Task Manager.
The best part is that you don’t need to change your script. You’ll just need to download and install NSSM from the official website. Once you have the nssm.exe file downloaded, run it to install the application. It’s best to add the folder to your path environment variable.
To create and run the services, launch the Windows terminal. Change the directory to where your Python script exists.
Enter the commands to install the script and then run it. Here’s an example of a script saved as Python_BackgroundScript.exe:
nssm.exe install Python_BackgroundScript C:\Users\User\Username\dist\Python_BackgroundScript.exe
nssm.exe start Python_BackgroundScript
Now, check if the service is running successfully. You can look for it in Task Manager.
Should there be issues, you can direct the error output to a file using the following line of code in the terminal:
nssm set Python_BackgroundScript AppStderr C:\Users\User\Username\service-error.log
You don’t need to create and run the service in the Windows terminal manually. You can use simple commands with NSSM and get the Python code running in the background. Now, every time Windows runs, the Python program will also run automatically.
Thanks to NSSM, you don’t even have to learn how to run a Python script as a service manually.
An Easier Alternative To Running Python Scripts for Scraping
You don’t need to be a coding expert to know how to run a Python script as a service. Instead of coding the script and using a terminal to run the service, you can use the Scraping Robot API.
A scraping API automatically collects website data and integrates it with other software through API calls. It automates the scraping process, eliminating the need to run the code manually whenever you need data from specific websites. All the data is organized, too.
Scraping Robot already has all the code for collecting data, so you don’t need to create your own. With a single API endpoint, you can send the HTTP request to collect data using the appropriate credentials. It’s simple, fast, and convenient, and makes scraping data easy for everyone.
Why use scraping robot?
Even if you’re confident you know how to run a Python script as a service or daemon for scraping, using a bot is a much better alternative. Here’s why.
Prebuilt and custom modules
Scraping Robot provides several models for parsing and scraping data that cater to different business needs. The prebuilt modules eliminate the need to define the kind of data you’re looking for.
Getting a custom module is also possible should you need more complex data. With these modules, you get the exact data you need from different websites. No more spending hours refining and filtering data.
Speedy automation
Scraping Robot scrapes data automatically based on the parameters defined. As a result, it can parse hundreds of web pages in minutes. Plus, little to no intervention is required as the program runs independently, collecting data and storing it in the appropriate location.
Easy to use
Not every business has a developer on their payroll. Scraping Robot API allows even those who aren’t tech-savvy to gather the data they need. As the API already has the code to execute the scraping bot, you don’t need to hire a developer or outsource one.
Try Scraping Robot Instead!
Now that you know how to run a Python script in Linux and Windows, you can see why Scraping Robot is the ideal solution. For both Linux and Windows, you must manually run the script as a service and add the necessary conditions to ensure it runs without disruption.
But even if you’re sure you know how to run a Python script in the background, errors are always a possibility. The Scraping Robot API provides a viable alternative. It’s cost-effective and convenient, ideal for eCommerce websites and companies that scrape websites to collect data on competitors and the market.
The information contained within this article, including information posted by official staff, guest-submitted material, message board postings, or other third-party material is presented solely for the purposes of education and furtherance of the knowledge of the reader. All trademarks used in this publication are hereby acknowledged as the property of their respective owners.
Related Articles
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.
In Python, you have a library named sched[1] 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()
Output:
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)
Output:
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.
#!/usr/bin/env python3
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
Wrap Up
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.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read:
- 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